Sequential function calls

Hi!

I’m not sure if I completely understood what you’re trying to accomplish, so let me talk through it a little bit to make sure I understood…

Elm is pure, so “calling a function” doesn’t have side-effects. And Elm, unlike Haskell, also doesn’t have a Monad interface to represent side-effects. So there’s not going to be any purpose to calling a function multiple times unless you are using different arguments. I’m thinking in your examples, you want to produce some values, and then use the values multiple times?

In the case of the ul example, you want to end up with two displayed copies of each card? If so, I’d do it like this:

let
    cardViews = List.map (initializeCards model.selectedCard) model.cards
in
Html.ul []
    (List.concat [cardViews, cardViews])

In your initializeN example, f would return the same value on every call, right? So then you can use List.repeat (and in your example, f isn’t passed an argument, so it’s really just a value, not a “function”):

List.repeat 5 f

Or if there were some arguments to f:

List.repeat 5 (f arg1 arg2 arg3)

If I’m wrong about the above and you do want different input values passed to each call, then you’d use List.map if you have a list of input values, possibly with List.range if you need to create a list of indices

cardFromIndex : Int -> Card
cardFromIndex i = ...

List.map cardFromIndex (List.range 1 52)

or List.indexedMap if you have a list of input values and want indices.

1 Like