简体   繁体   English

Haskell中的单引号和双引号

[英]Single vs. double quotes in Haskell

Consider: 考虑:

ghci> :t 'a'
'a' :: Char
ghci> :t "a"
"a" :: [Char]

Why is it treating single and double quotes differently, and is that important? 为什么不同地处理单引号和双引号,这是否重要?

This is just like C/C++/C#/Java and other programming languages. 这就像C / C ++ / C#/ Java和其他编程语言。 Single quotes means single character, double quotes means character array (string). 单引号表示单个字符,双引号表示字符数组(字符串)。

In Haskell, 'c' is a single character ( Char ), and "c" is a list of characters ( [Char] ). 在Haskell中, 'c'是单个字符( Char ), "c"是字符列表( [Char] )。

You can compare this to integers and lists of integers: 您可以将其与整数和整数列表进行比较:

ghci> let a = 1
ghci> let b = [1,2,3]
ghci> :t a
a :: Integer
ghci> :t b
b :: [Integer]

It is important because there has to be a distinction between single elements and a list of elements. 这很重要,因为必须区分单个元素和元素列表。 You can perform different operations on simple elements, and different operations on lists. 您可以对简单元素执行不同的操作,也可以对列表执行不同的操作。

They're interpreted differently because they just are (that's just how the language is defined). 它们的解释不同,因为它们只是(这就是语言的定义)。 This is important because a Char and a [Char] aren't the same thing. 这很重要,因为Char[Char]不是一回事。 Other than that I'm not sure what you're trying to ask here. 除此之外,我不确定你在这里要问什么。

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

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