Consider:
ghci> :t 'a'
'a' :: Char
ghci> :t "a"
"a" :: [Char]
Why is it treating single and double quotes differently, and is that important?
General Usage Rules In America, Canada, Australia and New Zealand, the general rule is that double quotes are used to denote direct speech. Single quotes are used to enclose a quote within a quote, a quote within a headline, or a title within a quote.
The apostrophe is just part of the name. It is a naming convention (idiom) adopted in Haskell. The convention in Haskell is that, like in math, the apostrophe on a variable name represents a variable that is somehow related, or similar, to a prior variable. x' is related to x , and we indicate that with the apostrophe.
The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string. It interprets the Escape sequences. Each variable will be replaced by its value.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With