views:

111

answers:

2

I'm doing a program in Haskell (on the Haskell platform), and I know I'm getting perfectly formatted inputs, so the input may look like

[ ['a'], ['b'], ['c'] ]

I want Haskell to be able to take this and use it as a list of it's own. And, I'd like this list to be over multiple lines, i.e., I want this to also work:

[
  ['a'],
  ['b'],
  ['c']
]

I can parse this input, but I've been told there's a way to do this easily - this is supposed to be the 'trivial' part of the assignment, but I don't understand it.

+9  A: 
read "[ ['a'], ['b'], ['c'] ]" :: [[Char]]

will return [ ['a'], ['b'], ['c'] ]. If you assign the result of read to a variable that can be inferred to be of type [[Char]], you don't need the :: [[Char]] bit.

sepp2k
+3  A: 

There is an instance of the Read class for Haskell lists, meaning you can use the read function to effectively parse strings formatted like Haskell lists, which is exactly what you have.

Martinho Fernandes