[Haskell-cafe] defining mapPairs function
alexteslin at yahoo.co.uk
Wed Aug 29 17:04:12 EDT 2007
> I just came across with this question on the exam and can not think of
> implementing it.
> mapPair :: (a -> a -> a) -> [a] -> [a]
> such that mapPairs f [x1, x2, x3, x4...] = [f x1 x2, f x3 x4,...]
> and if the list contains an odd number of elements, the last one is kept
> unchanged, for example
> mapPairs f [x1, x2, x3] = [f x1 x2, x3]
> Any ideas will be appreciated, thanks
Oh, I think i just defined it - seems to work.
I spent some time on the exam and made silly mistakes:
mapPairs :: (a -> a -> a) -> [a] -> [a]
mapPairs f [x] = [x]
mapPairs f  = 
mapPairs f (x:xs) = f x (head xs) : mapPairs f (tail xs)
I was concing f x to (head xs) as well and
mapPairs f [x] = x - this is very silly mistake.
Does anyone think this is the right answer?
View this message in context: http://www.nabble.com/defining-mapPairs-function-tf4350356.html#a12395696
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
More information about the Haskell-Cafe