让我们一起穿过它!
nextWord "an apple"
由于"an apple"
不确实的图案对阵[]
,我们在第二种情况下。在'a': "n apple"
代入c : cs
,我们得到:
nextWord ('a':"n apple")
| isSpace 'a' = ([], "n apple")
| otherwise = ('a': word, other)
where
(word, other) = nextWord "n apple"
isSpace 'a'
是False
,所以这简化为
nextWord ('a':"n apple") = ('a': word, other)
where (word, other) = nextWord "n apple"
同样,对于nextWord "n apple"
我们得到
nextWord ('n':" apple") = ('n': word, other)
where (word, other) = nextWord " apple"
而对于nextWord " apple"
我们得到
nextWord (' ':"apple")
| isSpace ' ' = ([], "apple")
| otherwise = ('a': word, other)
where
(word, other) = nextWord "n apple"
从而简化到
nextWord (' ':"apple") = ([], "apple")
代回到我们表达了nextWord "n apple"
,我们得到
nextWord ('n':" apple") = ('n': word, other)
where (word, other) = ([], "apple")
其简化为
nextWord ('n':" apple") = ('n':[], "apple")
或
nextWord ('n':" apple") = ("n", "apple")
现在代说回我们的表达式nextWord "an apple"
,我们得到
nextWord ('a':"n apple") = ('a': word, other)
where (word, other) = ("n", "apple")
其简化为
nextWord ('a':"n apple") = ('a':"n", "apple")
或
nextWord ('a':"n apple") = ("an", "apple")
“为什么要返回一个元组与第一元素是空的列表“。它应该返回什么呢? – 2014-12-27 19:04:08
你可以在Haskell中写下该值吗? – 2014-12-27 19:05:03
这不是元组递归。它是一个返回元组的递归函数。在空格中,它返回空列表作为第一个组件:这实际上是空字符串。这样做是为了使递归调用可以添加前面的字符,以隔离输入字符串中的第一个字。 – chi 2014-12-27 21:28:09