Lecture -*- Outline -*- * lexical matters in Haskell (Thompson 3.7, Davie 2.14, appendix C.2-3) ** important and unusual lexical conventions ------------------------------------------ IMPORTANT AND UNUSUAL LEXICAL CONVENTIONS ::= { } ::= { } ::= | | | ' ::= | _ ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ::= ::= A | B | C | D | ... | Z ::= a | b | c | d | ... | z - Case matters identifiers: fact, x y constructors: Rational, Stack - Layout (white space) matters: (if omit braces and semicolons, following "where", "let", "do", and "of") Example: let x = 3 y = 4 in x + y ==> let {x = 3 ;y = 4 }in x + y ------------------------------------------ For identifiers, unicode characters are also allowed simplified layout rules: following token determines indentation level => { same indentation as that token => ; less indentation => } Watch out, you can get syntax errors from bad indentation! see sections 2.7 and 10.3 of the Haskell report for details ** identifiers and operators (Thompson 1.7, Davie 2.14.2) ------------------------------------------ IDENTIFIERS (NOT INFIX) can use letters, digits, primes and underscores (_) CASE MATTERS varids: start with a-z examples _, pythag, fac, x, y', x3'n_, aGoodId conids: start with A-Z Examples: Stack, Rational, Typ'_3 ------------------------------------------ (unicode characters also allowed, unicode lowercase letters are small, unicode uppercase/titlecase letters are large) ------------------------------------------ OPERATORS Examples: +, -, !!, ++, ==, /= :, :# Notes: 1. Drawn from: !#$%&*+./<=>?@\^|-~: and unicode symbols and punctuation 2. An operator is a constructor if it starts with : 3. All operators are infix except unary negation (-) 4. Any identifer can be made into an infix operator using backquotes 3 `div` 4 5. An infix operator can be used as an identifier when put in parentheses (+) 3 4 ------------------------------------------ Because operator strings are maximal length strings of symbols, instead of 2*-3 you have to write 2*(-3) or 2 * -3.