integerList = [1] ++ integerList
(head (tail integerList))
我已经运行了这段代码,结果是
1
,它是无休止的递归。我试图弄清楚haskell如何计算这些函数。有人可以写下这个过程吗?我想形象化。谢谢!
最佳答案
head
返回列表的第一个元素。在这种情况下,它将返回列表tail integerList
的第一个元素。 tail
返回没有第一个元素的原始列表。原始列表是integerList
,绑定到[1] ++ integerList
。 ++
运算符连接两个列表,因此结果列表为1 : integerList
。将tail
应用于此列表将得到integerList
,因此tail integerList
只会产生integerList
。
回到开始:用tail integerList
替换integerList
(因为这是它的计算结果)以获得head integerList
。提醒:integerList
评估为1 : integerList
。将head
应用于1
。