# [Haskell-beginners] Is there an "unscan" function?

Kyle Murphy 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)
where
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
empty list.

-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:

> Hello-
>
> 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]
>
> Thanks,
> Jeffrey
> _______________________________________________
> Beginners mailing list