[GHC] #3631: Overload the Prelude iterate to support list input
GHC
trac at galois.com
Sun Nov 1 01:22:58 EST 2009
#3631: Overload the Prelude iterate to support list input
-----------------------------+----------------------------------------------
Reporter: shelbymoore3 | Owner:
Type: feature request | Status: new
Priority: normal | Component: Prelude
Version: 6.10.4 | Severity: normal
Keywords: | Testcase:
Os: Unknown/Multiple | Architecture: Unknown/Multiple
-----------------------------+----------------------------------------------
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.
--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/3631>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the Glasgow-haskell-bugs
mailing list