Haskell Cheat Sheet Thi s che at she et la ys out the fundame ntal ele- ments of the Haskell language: syntax, keywords and other elements. It is presented as both an ex- ecutable Haskell file and a printable document. Load the source into your favorite interpreter to play with code samples shown. Basic Syntax Comments A single line comment starts with ‘ - - ’ and extends to the end of the line. Multi- line comme nts start with ’ { - ’ and extend to ’ - } ’. Comments can be nested. Comments above function definitions should start with ‘ { - | ’ and those next to parameter types with ‘ - - ^ ’ for compatibility with Haddock, a sys- tem for documenting Haskell code. Reserved Wo rds The following words are reserved in Haskell. It is a syntax error to give a variable or a function one of these names. • c a s e • c l a s s • d a t a • d e r i v i n g • d o • e l s e • i f • i m p o r t • i n • i n f i x • i n f i x l • i n f i x r • i n s t a n c e • l e t • o f • m o d u l e • n e w t y p e • t h e n • t y p e • w h e r e Strings • " a b c " – Unicode st ring , sug ar for [ ' a ' , ' b ' , ' c ' ] . • ' a ' – Single character. Multi- line String s Normally, it is a syntax error if a string has any newline characters. That is, this is a syntax error: s t r i n g 1 = " M y l o n g s t r i n g . " Backslashes (‘ \ ’) can “escape” a newline: s t r i n g 1 = " M y l o n g \ \ s t r i n g . " The are a betw een the bac ksl ashes is ign ore d. Newlines in the string must be represented explic- itly: s t r i n g 2 = " M y l o n g \ n \ \ s t r i n g . " That is, s t r i n g 1 evaluates to: M y l o n g s t r i n g . While s t r i n g 2 evaluates to: M y l o n g s t r i n g . Esca pe Codes The following escape codes can be used in characters or string s: • \ n , \ r , \ f , etc. – The standard codes for new- line, carriage return, form feed, etc. are sup- ported. • \ 7 2 , \ x 4 8 , \ o 1 1 0 – A character with the value 72 in decimal, hex and octal, respectively. • \ & – A “null” escape character which allows numeric escape codes next to numeric liter- als. For e xample , \ x 2 C 4 is ∧ (in Unicode) while \ x 2 C \ & 4 is , 4 . This sequence cannot be used in character literals. Numbers • 1 – Integer or floating point value. • 1 . 0 , 1 e 1 0 – Floating point value. • 0 o 1 , 0 O 1 – Octal value. • 0 x 1 , 0 X 1 – Hexadecimal value. • - 1 – Negative number; the minus sign (“ - ”) cannot be separated from the number. Enumerations • [ 1 . . 1 0 ] – List of numbers – 1 , 2 , ... , 1 0 . • [ 1 0 0 . . ] – Infi nite list of number s – 1 0 0 , 1 0 1 , 1 0 2 , ... . • [ 1 1 0 . . 1 0 0 ] – Empty list, but [ 1 1 0 , 1 0 9 . . 1 0 0 ] will give a list from 110 to 100. • [ 0 , - 1 . . ] – Negative integers. • [ - 1 1 0 . . - 1 0 0 ] – Syntax error; need [ - 1 1 0 . . - 1 0 0 ] for negatives. • [ 1 , 3 . . 9 9 ] , [ - 1 , 3 . . 9 9 ] – List from 1 to 99 by 2, -1 to 99 by 4. In fact, any value which is in the E n u m class can be used: • [ ' a ' . . z ' ] – List of characters – a , b , ... , z . • [ ' z ' , ' y . . ' a ' ] – z , y , x , . . . , a . • [ 1 . 0 , 1 . 5 . . 2 ] – [ 1 . 0 , 1 . 5 , 2 . 0 ] . • [ U p p e r c a s e L e t t e r . . ] – List of G e n e r a l C a t e g o r y values (from D a t a . C h a r ). c 2010 Justin Bailey . 1 j g b a i l e y @ c o d e s l o w e r . c o m