[Haskell-cafe] Newb question about map and a list of lists

Bas van Dijk v.dijk.bas at gmail.com
Fri Sep 28 17:03:32 EDT 2007

On 9/28/07, Chuk Goodin <chukgoodin at gmail.com> 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...

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...



[1] Hoogle, The Haskell API Search Engine: http://haskell.org/hoogle

More information about the Haskell-Cafe mailing list