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

10-04 13:18