I'm learning Elm and trying to understand how to append a string to all items in a list, but at the end of each entry rather than the beginning. Sorry for the n00b question but I've read through all the docs/examples around List.map, String (.append, .join) and can't seem to find an answer.
e.g.
--create username list
usernames = ["Dave", "Simon", "Sally", "Joe"]
--create confirmExit function
confirmExit name = String.append " has left the room" name
--apply confirmExit function to all items in username list
List.map (\x -> confirmExit x) usernames
Gives me:
["has leftDave","has leftSimon","has leftSally","has leftJoe"] : List String
But how would I make it so that it returned:
["Dave has left","Simon has left","Sally has left","Joe has left"] : List String
Is there an equivalent of .append to add to the end instead of the beginning? Please?!
You just have the parameters reversed, try:
confirmExit name = String.append name " has left the room"
From the docs:
append : String -> String -> String
Append two strings. You can also use the (++) operator to do this.
append "butter" "fly" == "butterfly"
So you could also use:
confirmExit name = name ++ " has left the room"
Which is possibly a bit more readable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With