Haskell :: Recursion for List of Tuples -
i mapping tuple , managed make work single tuple (a, a). when i've changed tuple [[(a, a)]]
, says "couldn't match expected type of [(a, a)]
(a, a)
.
maptuple :: (a -> b) -> [[(a, a)]] -> [(b, b)] maptuple f [] = [] maptuple f ((a1, a2) : xs) = (f a1, f a2) : maptuple f xs myfunc :: int -> int myfunc xk = xk + 1 mytup = [[(1,2),(5,6),(9,10)]] :: [[(int, int)]] test = maptuple myfunc mytup
couldn't break error yet. missing here. in advance help.
of course maptuple won't work when change type [[(a,a)]]
because ((a1, a2) : xs)
still has type [(a,a)]
.
if want apply maptuple list of lists of tuples rather list of tuples don't need change maptuple @ all.
let's @ types second: maptuple (before changed it) had type (a -> b) -> [(a,a)] -> [(b,b)]
partially applying maptuple myfunc [(a,a)] -> [(b,b)]
from comments want function takes [[(1,1),(2,2)],[(3,3)]] :: [[(a,a)]]
, returns [[(2,2),(3,3)],[(4,4)]] :: [[(b,b)]]
so want take partial function , apply every [(a,a)]
in [[(a,a)]]
[[(b,b)]]
.
so overall want function type: ([(a,a)] -> [(b,b)]) -> [[(a,a)]] -> [[(b,b)]]
this can simplify (a -> b) -> [a] -> [b]
. there function us! regular old map.
so using old maptuple type (a -> b) -> [(a, a)] -> [(b, b)]
correct result following:
test = map (maptuple myfunc) mytup
hopefully makes sense, if not feel free ask clarification :)
Comments
Post a Comment