Fwd: [Haskell-cafe] turning an imperative loop to Haskell
Stephen Dolan
stedolan at gmail.com
Fri Sep 7 08:04:33 EDT 2007
Sorry, forgot to cc list
> 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
How about this:
import Data.List -- for transpose
f = sum
zipWithN fn = map fn . transpose
us k = s
where
s = [2..k+1] ++ (zipWithN f $ x $ map (flip drop s) [0..k-1])
take 10 (us 1)
[2,2,2,2,2,2,2,2,2,2]
take 10 (us 2)
[2,3,5,8,13,21,34,55,89,144]
take 10 (us 3)
[2,3,4,9,16,29,54,99,182,335]
take 10 (us 4)
[2,3,4,5,14,26,49,94,183,352]
We can always take it further :D
Stephen
More information about the Haskell-Cafe
mailing list