Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute next element of list based on previous elements

Tags:

haskell

I want to define an infinite list where each element is a function of all the previous elements.

So, the n+1th element of the list would be f [x1, x2, ..., xn].

This seems simple, but I cannot seem to get my head around how to do it. Can anyone help?

like image 297
Cameron Martin Avatar asked Feb 02 '15 18:02

Cameron Martin


1 Answers

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

Or

gen f = fix $ map f . inits
like image 112
user3237465 Avatar answered Sep 26 '22 22:09

user3237465