Using perfectly formatted input as a list in Haskell

I am making a program in Haskell (on the Haskell platform) and I know I am getting fully formatted inputs, so the input might look like

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

      

I want Haskell to be able to take this and use it as its own list. And, I would like this list to consist of several lines, i.e. I want this to work as well:

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

      

I can parse this input, but I was told that it is easy to do - it should be a "trivial" part of the assignment, but I don't understand it.

+2


a source to share


2 answers


read "[ ['a'], ['b'], ['c'] ]" :: [[Char]]

      



will return [ ['a'], ['b'], ['c'] ]

. If you are assigning the read result to a variable that can be defined as a type [[Char]]

, you don't need a bit :: [[Char]]

.

+9


a source


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



+3


a source







All Articles