Hi there,

for my master thesis I need to formally define the Elm language. (more on that at a later date) I really want to say that Elm has no run-time errors, that would make things a lot easier. From what I understand Elm uses Recursion without checking its termination and therefore will always run into run-time errors.

I would like to use this topic to get a feeling for how many people actually use recursive functions in their code, why and if it could be avoided/if I can ignore recursive functions in my formal definition.

How often do you use recursive function calls?

Could these recursive function be also written using fold, map, filter ?

? If so, why do you choose recursive function instead? Is it like in my find example, where I’m just optimizing efficiency or does it have other reasons?

So I’ll start with my experience with recursive functions. The only time I really needed to use them was when I wanted to exit out of the fold-function:

{-| calling `find true` will always run though the entire list -} find : (a -> Bool) -> List a -> Maybe a find condition = List.filter condition >> List.head {-| Now the function actually aborts once it has found a valid candidate. -} findRec : (a -> Bool) -> List a -> Maybe a findRec condition list = case list of head :: tail -> if head |> condition then Just head else findRec condition tail [] -> Nothing

For function where I my self don’t know when it stops, I usually use the update-function for each iteration:

update : Msg -> Model -> (Model, Cmd Msg) update msg ({x} as model) = case msg of Approximate -> let newX = iterate x in ( {model| x = newX} , if newX |> isGoodEnough(x) then Cmd.none else Task.perform (always Approximate) (Task.succeed ())

It would be interesting to know how many of you also use the update-function like I do.