[Haskell-cafe] defining mapPairs function

Max Vasin max.vasin at gmail.com
Wed Aug 29 23:56:59 EDT 2007


2007/8/30, Neil Mitchell <ndmitchell at gmail.com>:
> Hi
>
> > mapPairs :: (a -> a -> a) -> [a] -> [a]
> > mapPairs f [x] = [x]
> > mapPairs f [] = []
> > mapPairs f (x:xs) = f x (head xs) : mapPairs f (tail xs)
>
> It looks like it works, but you can get a better version by changing
> the last line:
>
> mapPairs f (x:y:zs) = ... - left as an exercise, but no need for head or tail.

And you can get less equations if you replace first two equations with
mapPairs f x = x and place it after Neil's.

-- 
WBR,
Max Vasin

JID: maxvasin at jabber.ru


More information about the Haskell-Cafe mailing list