【发布时间】:2019-12-02 18:38:01
【问题描述】:
我想做的是定义一个这样的函数:
[f 0, f f 0, f f f 0, f f f f 0, f f f f f 0..]
或者换句话说,每个元素都是通过函数运行的最后一个元素。
我已经尝试了几次,以类似于我在 Haskell 中看到的斐波那契数列的方式,通过调用具有预定义的前几个元素的列表:
fib = 0 : 1 : zipWith (+) fib (tail fib)
ls = 0 : f 0 : f (last ls)
如果我像这样将 f 定义为一个简单的 addOne 函数:
f = (+ 1)
我收到此错误:
<interactive>:124:5: error:
* Occurs check: cannot construct the infinite type: a ~ [a]
Expected type: [[a]]
Actual type: [a]
* In the expression: 0 : (f 0) : f (last y)
In an equation for `y': y = 0 : (f 0) : f (last y)
* Relevant bindings include
y :: [[a]] (bound at <interactive>:124:1)
我如何创建一个具有类似功能的列表?
【问题讨论】:
标签: list haskell recursion iteration higher-order-functions