1

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.

2
  • 1
    Are you sure that the input is not [ ['a'], ['b'], ['c'] ] (i.e. comma-separated) because then it would be trivial (using read). Commented May 17, 2010 at 21:53
  • Sorry, I updated it, that's correct Commented May 17, 2010 at 21:55

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.