[Haskell-cafe] turning an imperative loop to Haskell
ithika at gmail.com
Thu Sep 6 10:52:21 EDT 2007
On 06/09/07, Sebastian Sylvan <sebastian.sylvan at gmail.com> wrote:
> foo = 2 : 3 : zipWith f (drop 1 foo) foo
> There's also zipWith3 etc. for functions with more arguments.
I think this is called taking a good thing too far, but cool too:
f1 u = u + 1
f2 u v = u + v
f3 u v w = u + v + w
-- functions renamed for consistency)
zipWith1 = map
zipWith2 = zipWith
-- and hey presto!
us1 = 3 : zipWith1 f1 us1
us2 = 2 : 3 : zipWith2 f2 (drop 1 us2) us2
us3 = 2 : 3 : 4 : zipWith3 f3 (drop 2 us3) (drop 1 us3) us3
*Main> take 10 us1
[3,4,5,6,7,8,9,10,11,12] -- integers from three upwards
*Main> take 10 us2
[2,3,5,8,13,21,34,55,89,144] -- fibonacci
*Main> take 10 us3
[2,3,4,9,16,29,54,99,182,335] -- what's this?
More information about the Haskell-Cafe