String 哈斯克尔尾裂

String 哈斯克尔尾裂,string,haskell,split,tail,String,Haskell,Split,Tail,这就是我想做的: 如果我给 $hello world我会得到world 我现在做的是: tail(unwords(单词“$hello world”) 但是我得到的是“hello world”而不是world 我该怎么做才能把它做好呢?你必须在tail之后应用unwords,而不是反过来 预期的步骤顺序(可能)如下: 将字符串分解为单词列表 从列表中删除第一个单词 将剩余的单词连接到字符串 按照这种方式,您可以拆分并立即重新连接单词,然后删除结果字符串的第一个字符(因为字符串只是一个字符列表)。您

这就是我想做的:

如果我给
$hello world
我会得到
world

我现在做的是:

tail(unwords(单词“$hello world”)
但是我得到的是
“hello world”
而不是
world


我该怎么做才能把它做好呢?

你必须在
tail
之后应用
unwords
,而不是反过来

预期的步骤顺序(可能)如下:

  • 将字符串分解为单词列表
  • 从列表中删除第一个单词
  • 将剩余的单词连接到字符串

  • 按照这种方式,您可以拆分并立即重新连接单词,然后删除结果字符串的第一个字符(因为字符串只是一个字符列表)。

    您要做的是

    unwords $ tail $ words "$hello world"
    
    在GHCi工作我们得到

    > words "$hello world"
    ["$hello", "world"]
    > tail $ words "$hello world"
    ["world"]
    > unwords $ tail $ words "$hello world"
    "world"
    

    正如fjh正确指出的,字符串数组
    [“$hello”,“world”]
    被重新连接成字符串
    “$hello world”
    尾部
    ,然后切掉第一个字符
    $
    。我建议使用函数
    last
    而不是
    tail$unwords
    ,这将从单词数组
    world
    中获取最后一个元素

    Prelude> last $ words $ "$hello world"
    "world"
    

    虽然我喜欢你指出OP做错了什么,但从技术上讲,这是缺少问题的解决方案+1无论如何。@Waldheinz:不正确。答案是答案的第一行。