简体   繁体   中英

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. 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] ).

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. Other than that I'm not sure what you're trying to ask here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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