Using my zip function(see below), write a function addpairs that takes two lists [x0; x1; … ; xn] and [y0; y1; … ; ym] and returns the list [x0 + y0; x1 + y1; … ; xp + yp], where p = min(n; m). Where this is the output from console.
*Recursion> :t addpairs
addpairs :: (Num a) => [a] -> [a] -> [a]
*Recursion> addpairs [1,2,3,4] [40,50,60,90]
[41,52,63,94]
my zip function:
zip [] _ = []
zip _ [] = []
zip (a:aa) (b:bb) = (a,b) : Recursion.zip aa bb
Any help would be great.
I actually solved this problem
This was the solution I came up with:
addpairs xs ys = map f (Recursion.zip xs ys)
where f (x,y) = x + y
Thanks for all that helped.
If
xsandysare the two lists your function is given, thenzip xs yswill be a list of tuples of numbers to add:zip xs yswill have the type[(a,a)]for some numeric typea. Next, take a look at the type ofmap:Your function should look like this:
where
fshould be a function taking an(a, a)to be added and returning the resultinga. That should help you fill in the rest.