Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elm - mapping a list and appending string to the end of each item

Tags:

elm

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?!

like image 514
Wilfo Avatar asked Dec 11 '25 16:12

Wilfo


1 Answers

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

like image 190
Murph Avatar answered Dec 14 '25 03:12

Murph