榆树 - 映射列表并将字符串附加到每个项目的末尾

Wil*_*lfo 3 elm

我正在学习Elm,并试图了解如何将字符串附加到列表中的所有项目,但是在每个条目的末尾而不是开头.很抱歉n00b问题,但我已经阅读了List.map,String(.appel,.join)周围的所有文档/示例,似乎无法找到答案.

例如

--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
Run Code Online (Sandbox Code Playgroud)

给我:

["has leftDave","has leftSimon","has leftSally","has leftJoe"] : List String
Run Code Online (Sandbox Code Playgroud)

但是如何让它返回:

["Dave has left","Simon has left","Sally has left","Joe has left"] : List String
Run Code Online (Sandbox Code Playgroud)

是否有相应的.append添加到结尾而不是开头?请?!

Mur*_*rph 5

你只需要反转参数,试试:

confirmExit name = String.append name " has left the room"

来自文档:

append:String - > String - > String

附加两个字符串.您也可以使用(++)运算符来执行此操作.

append "butter" "fly" == "butterfly"

所以你也可以使用:

confirmExit name = name ++ " has left the room"

哪个可能更具可读性