Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single vs. double quotes in Haskell

Tags:

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?

like image 282
Marty Wallace Avatar asked Apr 07 '14 13:04

Marty Wallace


People also ask

What is the difference between a single and double quote?

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.

What do apostrophes do in Haskell?

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.

What is the difference between single and double quotes to delineate strings?

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.


2 Answers

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.

like image 60
Ove Avatar answered Oct 23 '22 18:10

Ove


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.

like image 25
Cubic Avatar answered Oct 23 '22 19:10

Cubic