这是GHCi会议:
Prelude> words " one two three"
["one","two","three"]
Prelude> lines "\none\ntwo\nthree"
["","one","two","three"]
这种不一致是否有原因?如果是这样,它是什么?
答案 0 :(得分:9)
lines
是一个实际的广告素材:您可以使用它来分割'\n'
字符处的任何字符串,然后使用unlines
完美地重新组合它们。 (好吧,差不多:让我们忽略尾随新行和Windows行结束。)
如果words
仅使用' '
而不是'\n'
作为分隔符,则行为相同,那么它就不会按照我们想要的方式运行:例如,串
"I will not buy this record\nit is scratched"
将分裂为
["I","will","not","buy","this","record\nit","is","scratched"]
words
通过拆分任何空格来避免。
Prelude> words "I will not buy this record\nit is scratched"
["I","will","not","buy","this","record","it","is","scratched"]
这意味着 a)它无论如何都不是双射,因为空白的味道会丢失,而 b)你会得到很多“空字“当两个空格字符相邻时。
因此,words
的明智行为就是将这样的空白压缩成一个空白。