[Haskell-beginners] Is there an "unscan" function?
orclev at gmail.com
Thu Jan 12 07:41:46 CET 2012
I was able to build something incredibly convoluted that accomplishes what
you want, but I'm sure there's a better way to do it.
unscanl1 :: (a -> a -> a) -> [a] -> [a]
unscanl1 f xs = (head xs) : (map (\(a:b:_) -> f a b) $ convert xs)
convert = takeWhile (\xs -> length xs == 2) . map (take 2) . tails
I'm also not sure if this works in the general case, but it worked with the
example you gave and a couple other quick test cases I thought up. As with
any case where you use head, bad stuff will happen if feed an empty list,
so either add a case that matches on  or make sure not to feed it an
-R. Kyle Murphy
Curiosity was framed, Ignorance killed the cat.
On Wed, Jan 11, 2012 at 23:44, Jeffrey Thornton
<jeffreyjthornton at gmail.com>wrote:
> Is there standard function in Haskell that effectively does an inverse
> scan? For example,
> scanl1 (\ x y -> x+y) [1,2,3,4] == [1,3,6,10].
> So is there a very simple built-in way to do this hypothetical example?:
> unscanl1 (\ x y -> y-x) [1,3,6,10] == [1,2,3,4]
> Beginners mailing list
> Beginners at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners