在几种编程语言(包括 JavaScript、Python 和 Ruby)中,可以在自身内部放置一个列表,这在使用列表来表示无限详细的分形时很有用。但是,我尝试在 Haskell 中执行此操作,但没有按预期工作:
--aList!!0!!0!!1 should be 1, since aList is recursively defined: the first element of aList is aList.
main = putStrLn $ show $ aList!!0!!0!!1
aList = [aList, 1]
程序没有打印1
,而是产生了这个编译器错误:
[1 of 1] Compiling Main ( prog.hs, prog.o )
prog.hs:3:12:
Occurs check: cannot construct the infinite type: t0 = [t0]
In the expression: aList
In the expression: [aList, 1]
In an equation for `aList': aList = [aList, 1]
是否可以像我在这里尝试做的那样在 Haskell 中放置一个列表?