[Haskell-cafe] Does somebody know about these functions?
Johan Holmquist
holmisen at gmail.com
Tue Feb 28 20:21:00 CET 2012
>> inter :: (a -> a -> b) -> [a] -> [b]
>> inter f [] = []
>> inter f l = map (uncurry f) $ zip l (tail l)
>
> This is the same as
>
> inter :: (a -> a -> b) -> [a] -> [b]
> inter f l = zipWith f l (tail l)
Except when l == [], but the second equation can be replaced by this nicer one.
> and you can use it to define the good old Fibonacci sequence:
>
> fibs = 0 : 1 : inter (+) fibs
Another use :-)
(sorry Holger for duplicate -- hit wrong answer button at first)
Together, these functions can be used to define a variant of groupBy
that does the "expected thing" in the case of groupBy (<) for example.
groupBy f l = gby $ zip (undefined : inter f l) l where
gby [] = []
gby ((_,x):ps) = withPair (:) ((x:) . map snd) gby (span fst ps)
>>> groupBy (<) [1,2,3, 2,3, 1,2]
[[1,2,3],[2,3],[1,2]]
/Johan
More information about the Haskell-Cafe
mailing list