#3631: Overload the Prelude iterate to support list input
---------------------------------+------------------------------------------
Reporter: shelbymoore3 | Owner:
Type: feature request | Status: closed
Priority: normal | Milestone:
Component: Prelude | Version: 6.10.4
Severity: normal | Resolution: wontfix
Keywords: | Difficulty: Unknown
Testcase: | Os: Unknown/Multiple
Architecture: Unknown/Multiple |
---------------------------------+------------------------------------------
Comment (by shelbymoore3):
> > Has anyone pointed out yet that your 'iterate' function is non-
terminating?
Ah, I see your point now. I had a typo in my function, here is the
obvious correction:
{{{
iterate f x = x ++ iterate f (x ++ (f x))
}}}
I thought you meant that it was infinitely recursive (which obviously
standard Prelude is also), then I realized you meant that the function
never returned a list. I wish you would have said that.
Actually I should modify the proposal and my usage to be more consistent
with the current Prelude `iterate` and for maximum generality:
{{{
iterate f x = x ++ iterate f (f x)
}}}
Then my usage example changes to:
{{{
fibs = iterate (\x -> x ++ [last x + last init x]) [ 0 : 1 ] where
}}}
Actually I think the way to solve the collision with standard Prelude is
to make an overload of `iterate` that is even more general (but then this
requires the dreaded type inference or `a` meaning also `[a]`), so thus I
don't recommend this:
{{{
iterate :: (a -> a -> [a]) -> (a -> a) -> a -> [a]
iterate op f x = x 'op' iterate f (f x)
}}}
{{{
fibs = iterate (++) (\x -> x ++ [last x + last init x]) [ 0 : 1 ] where
}}}
--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/3631#comment:13>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs