This posting was inspired by the ideas in ‘Composing Fractals’ by Mark P Jones. Fractals are generated by applying a function to the result of applying that function to the result of applying that function… Haskell has a very simple way of expressing this iterative function application:
1 |
iterate :: (a -> a) -> a -> [a] |
i.e. iterate takes a function, that takes an ‘a’ and produces an ‘a’, along with an initial ‘a’ – the seed value. The function is then repeatedly applied ‘forever’ giving an infinite list of [a].
1 2 |
-- here x is the seed iterate f x == [x, f x, f (f x), ...] |
Of course we can’t really use all of an infinite list so we apply Haskell’s ‘take’ Read More