开发者

Using perfectly formatted input as list in Haskell

开发者 https://www.devze.com 2022-12-30 09:20 出处:网络
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

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 und开发者_如何学Cerstand it.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消