There are several ways to do it, but one is:
gen :: (a -> a) -> a -> [a]
gen f s = s : gen f (f s)
This function takes a functon f
and some valus s
and returns s
, after wich it calls itself with that same f
, and the result of f s
. Demonstration:
Prelude> :t succ
succ :: (Enum a) => a -> a
Prelude> let gen f s = s : gen f (f s)
Prelude> take 10 $ gen succ 3
[3,4,5,6,7,8,9,10,11,12]
In the above example succ
acts as the function generate :: Integer -> Integer
which you mention. But observe that gen
will work with any function of type a -> a
.
Edit: and indeed, gen
is identical to the function iterate
from the Prelude (and Data.List).