I’m really new to Haskell and I’m stuck on trying to map the first item of each pair in a list.
Obviously this works:
map :: (a -> b) -> [a] -> [b]
map f xs = [f x | x <- xs]
But how do I get it to work for
map :: (a -> b) -> [(a, Int)] -> [b]
I just want it to ignore the Int values for now and apply f to a like it does in the first example. I’ve been trying for ages now so thanks for any help.
Well, assuming you don’t want to use the build-in function
map, starting from this:To accept a list of type
[(a, Int)]and use just thea, you can pattern match the tuple:If you want to keep the
Int, you can put it back together afterwards:But all of this is a bit redundant. You can do the same by changing the argument to the original, generic
map:For the third version, the standard library’s module
Control.Arrowgives you a function calledfirstthat can be used to get the same effect:Neat, huh?