简体   繁体   English

在Haskell中使用完美格式化的输入作为列表

[英]Using perfectly formatted input as list in Haskell

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 我在Haskell(在Haskell平台上)做一个程序,我知道我得到了完美格式化的输入,所以输入可能看起来像

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

I want Haskell to be able to take this and use it as a list of it's own. 我希望Haskell能够接受它并将其用作它自己的列表。 And, I'd like this list to be over multiple lines, ie, 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. 我可以解析这个输入,但是我被告知有一种方法可以很容易地做到这一点 - 这应该是作业的“琐碎”部分,但我不明白。

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

will return [ ['a'], ['b'], ['c'] ] . 将返回[ ['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. 如果将读取结果分配给可以推断为[[Char]]类型的变量,则不需要:: [[Char]]位。

Haskell列表中有一个Read类的实例,这意味着您可以使用read函数有效地解析格式化为Haskell列表的字符串,这正是您所拥有的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM