Skip to content

Overload the Prelude iterate to support list input

Had to overload the Prelude function iterate to support list input. The overload could be global (should be added to Prelude), no need to wrap in where. My example fibonacci sequence usage follows.

    fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

Make forward iteration explicit, and remove self referential space "leak" above due direct recursion on fibs.

    fibs = iterate (\x -> last x + last init x) [ 0 : 1 ] where
    	iterate :: ([a] -> [a]) -> [a] -> [a]
    	iterate f x =  iterate f (x : (f x))
    	-- iterate f x == [x, f x, f (f x), ...]

Or verbosely:

        fibs = iterate (\x -> fib -1 + fib -2 where fib i = | i==-1=last x | i==-2=last init x) [ 0 : 1 ]
        	-- negative indices in local function fib offset from end of list

P.S. Note I not using HUGS nor GHC, this is just in my head. The above are my unique solutions, didn't lift them from www. I am just learning FP and Haskell for first time in my head past few days. So this might be rubbish.

Trac metadata
Trac field Value
Version 6.10.4
Type FeatureRequest
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Prelude
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information