Newb question about map and a list of lists

I have a list of lists of pairs of numeric Strings (like this: [["2","3"],["1","2"],["13","14"]] etc.) I'd like to change it into a list of a list of numbers, but I'm not sure how to go about it. If it was just one list, I could use map, but map.map doesn't seem to work. Any suggestions, or pointers to a reference online? thanks in advance, -- chuk

On Fri, 2007-09-28 at 11:19 -0700, Chuk Goodin wrote:
I have a list of lists of pairs of numeric Strings (like this: [["2","3"],["1","2"],["13","14"]] etc.) I'd like to change it into a list of a list of numbers, but I'm not sure how to go about it. If it was just one list, I could use map, but map.map doesn't seem to work. Any suggestions, or pointers to a reference online?
map :: (a -> b) -> [a] -> [b], so map . map :: (a -> b) -> [[a]] -> [[b]] which is right. Just be sure to put it in parentheses before you apply it to anything. If you need more help, more information on what you did and what happened would be appreciated. jcc

Chuk Goodin wrote:
I have a list of lists of pairs of numeric Strings (like this: [["2","3"],["1","2"],["13","14"]] etc.) I'd like to change it into a list of a list of numbers, but I'm not sure how to go about it. If it was just one list, I could use map, but map.map doesn't seem to work. Any suggestions, or pointers to a reference online?
Your instinct to use map.map is correct. Just be careful to write: (map.map) read l or map.map $ read l beware that: map.map read l is parsed as map.(map read l)... Jules

On 9/28/07, Chuk Goodin
I have a list of lists of pairs of numeric Strings (like this: [["2","3"],["1","2"],["13","14"]] etc.) I'd like to change it into a list of a list of numbers...
Now that you know (map . map) which Jonathan explained you need to apply that to a function that converts 'String's to 'Int's. You can write this function yourself or use hoogle [1] to search for a function in the standard library that does what you want. (If you now what type classes are you don't have to read any further) Hint: the reverse of the function you are looking for is called 'show' and its type is 'Show a => a -> String'. Why isn't the type of 'show' just 'Int -> String'? Well you probably not only want to convert Ints to Strings but also Floats to Strings, Doubles to Strings, Foo's to String, Bars to Strings, ... So you want to have a function called 'show' that is "overloaded" to work on any type that can be 'Show'n. To declare an overloaded function in Haskell you should define a type class, like this: class Show a where show :: a -> String This declares a class of types called 'Show' for which a function 'show' is defined which converts a value of that type into a 'String'. Now you should provide actual definitions of 'show' for the types you want to show. These are called instance declarations: instance Show Int where show n = ... code that actually converts the Int 'n' to a 'String'... instance Show Float where show f = ... code that actually converts the Float 'f' to a 'String'... Now you can use the function 'show' to convert Ints and Floats to Strings. Nice! Of course you are looking for the reverse of 'show', good luck searching... regards, Bas. [1] Hoogle, The Haskell API Search Engine: http://haskell.org/hoogle
participants (5)
-
Bas van Dijk
-
Brent Yorgey
-
Chuk Goodin
-
Jonathan Cast
-
Jules Bean