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

Stephen Tetley stephen.tetley at gmail.com
Fri Jan 13 19:19:49 CET 2012


Hi Jeffrey

My version actually contains an error, it should be:

unscan :: (a -> a -> b) -> [a] -> [b]
unscan f (a:b:bs) = f a b : unscan f (b:bs)
unscan _ _        = []

A slightly less concise, but optimized version avoids putting the
second element back in a list:

unscan f (a:b:bs) = f a b : go b bs
  where
    go _ [] = []
    go x (z:zs) = f x z : go z zs
unscan _ _        = []

The first mention of "speed" for traversing lists at different rates
I've seen was in the paper "There and Back Again" by Olivier Danvy and
Mayer Goldberg.

http://www.brics.dk/RS/02/12/BRICS-RS-02-12.pdf

Best wishes

Stephen



More information about the Beginners mailing list