这就是我想要做的事情:
如果我给
$hello world
我会得到world
我正在做的是:
tail (unwords (words "$hello world"))
但我得到"hello world"
而不是world
我该怎样做才能做到正确?
答案 0 :(得分:8)
您必须在unwords
之后应用tail
,而不是相反。
预期的步骤顺序(可能)如下:
你这样做,你拆分并立即重新加入单词,然后你只删除结果字符串的第一个字符(因为字符串只是一个字符列表)。
答案 1 :(得分:2)
你想要做的是
unwords $ tail $ words "$hello world"
在GHCi中完成它我们得到了
> words "$hello world"
["$hello", "world"]
> tail $ words "$hello world"
["world"]
> unwords $ tail $ words "$hello world"
"world"
答案 2 :(得分:0)
正如fjh所说,字符串数组["$hello", "world"]
重新加入字符串"$hello world"
和tail
,然后切掉第一个字符$
。我建议使用函数last
而不是tail $ unwords
,它将从单词world
数组中获取最后一个元素。
Prelude> last $ words $ "$hello world"
"world"