Reputation: 331
I have the function
getCode :: [(a, Int)] -> [a]
getCode = concatMap (uncurry replicate)`
I expect
getCode [(‘a’,4),(‘b’,1),(‘a’,3),(‘b’,1)]
to output
“aaaabaaab”
Instead I get an error that it does not match with the expected data type [(a,Int)]
, and the actual data type is [(Int,a)]
. How do i change it to make it work? Or any other way?
Upvotes: 2
Views: 1523
Reputation: 33466
You can swap the parameters of replicate
with the flip
function:
flip :: (a -> b -> c) -> b -> a -> c
flip f
takes its (first) two arguments in the reverse order off
.
getCode :: [(a, Int)] -> [a]
getCode = concatMap (uncurry (flip replicate))
Or use swap
from Data.Tuple
to do the same thing with a tuple instead of function parameters:
getCode :: [(a, Int)] -> [a]
getCode = concatMap (uncurry replicate . swap)
Upvotes: 2