根据以前的元素计算列表的下一个元素

时间:2015-02-02 18:23:02

标签: haskell

我想定义一个无限列表,其中每个元素都是前面所有元素的函数。

因此,列表中的n+1元素将为f [x1, x2, ..., xn]

这看起来很简单,但我似乎无法理解如何做到这一点。有人可以帮忙吗?

3 个答案:

答案 0 :(得分:10)

gen f = xs where xs = map f $ inits xs

或者

gen f = fix $ map f . inits

答案 1 :(得分:4)

作为另一个答案的替代方案,希望更具可读性但不那么简洁:

-- "heads f" will generate all of the the prefixes of the inflist
heads f = map ( (flip take) (inflist f) ) [1..]
-- inflist will generate the infinite list
inflist f = ( f [] ) : map f (heads f)

-- test function
sum1 s = 1 + sum s

-- test run
>> take 5 (inflist sum1)
[1,2,4,8,16]

UPD:  如上所述,heads函数可以替换为inits,我不知道它存在。

答案 2 :(得分:2)

您可以使用unfoldr

import Data.List

gen :: ([a] -> a) -> a -> [a]
gen f init = unfoldr (\l -> let n = f (reverse l) in (Just (n, n:l))) [init]

请注意,每次都必须反转输入列表。您可以改为使用Data.Sequence

import Data.Sequence

genSeq :: (Seq a -> a) -> a -> Seq a
genSeq f init = unfoldr (\s -> let n = f s in (Just (n, s |> n))) (singleton init)
相关问题