简体   繁体   English

haskell中的字符串vs字符串?

[英]string vs String in haskell?

For type declarations as here , String is used: 对于此处的类型声明,使用String

p_pair :: CharParser () (String, Maybe String)

And in some other places as here , string is used: 这里的其他一些地方,使用string

req = ctor <$ string name <* char ' '

Note the capitalization difference between String and string 请注意Stringstring之间的大小写差异

What is the difference between String and string in Haskell? Haskell中Stringstring什么区别?

In Haskell value and type variable names start with a lower-case letter while value and type constructor names, type synonyms, and classes start with a capital letter. 在Haskell中, 值和类型变量名称以小写字母开头,而值和类型构造函数名称,类型同义词和类以大写字母开头。 For example: 例如:

data Tree a = Empty
            | Node a (Tree a) (Tree a)

height Empty = 0
height (Node _ l r) = max (height l) (height r) + 1

Note that Tree is a type constructor name, Empty and Node are value constructor names and height is a function name. 请注意, Tree是类型构造函数名称, EmptyNode是值构造函数名称, height是函数名称。 In your case String is the name of the type "string" which is simply a synonym for [Char] , while string is a function. 在你的情况下, String是“string”类型的名称,它只是[Char]的同义词,而string是一个函数。

Looks like string is the function from Text.Parsec.Char while String is the usual Type. 看起来stringText.Parsec.Char的函数,而String是常用的Type。

In fact, in the same chapter of RealWorldHaskell , this function is explicitly mentioned under 'Choices and Errors;: 实际上,在RealWorldHaskell的同一章中,在“选择和错误”中明确提到了这个函数:

This must be done carefully. 这必须小心。 Recall that our earlier definition of eol was simply char '\\n'. 回想一下,我们之前对eol的定义只是char'\\ n'。 There is a parser called string that we can use to match the multi-character patterns. 我们可以使用一个名为string的解析器来匹配多字符模式。 Let's start by thinking of how we would add support for \\n\\r 让我们首先考虑如何添加对\\ n \\ r \\ n的支持

一个是类型 ,另一个是函数

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

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