我正在学习Elm并尝试了解如何将字符串附加到列表中的所有项目,但是在每个条目的末尾而不是开头。对于n00b问题很抱歉,但我已经阅读了List.map,String(。appel,.join)周围的所有文档/示例,但似乎无法找到答案。
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
给我:
["has leftDave","has leftSimon","has leftSally","has leftJoe"] : List String
但我怎么做才能让它返回:
["Dave has left","Simon has left","Sally has left","Joe has left"] : List String
是否有相当于.append添加到结尾而不是开头?请?!
答案 0 :(得分:5)
您只需反转参数,请尝试:
confirmExit name = String.append name " has left the room"
来自docs:
追加:字符串 - >字符串 - >字符串
附加两个字符串。你也可以 使用(++)运算符来执行此操作。
append "butter" "fly" == "butterfly"
所以你也可以使用:
confirmExit name = name ++ " has left the room"
可能更具可读性