Basic types and definitions Chapter 3 of Thompson
Booleans [named after logician George Boole] Boolean values True and False are the result of tests are two numbers equal is one smaller than the other The Haskell type is called Bool. The language predefines the following Boolean operators && logical and logical or (inclusive) not logical negation == equality /= inequality
Truth tables t 1 t 2 t 1 && t 2 t 1 t 2 not t 1 True True True True False False True False True True True False False True False False False False
Boolean function definition: exclusive or exor :: Bool -> Bool -> Bool exor x y = (x y) && not (x && y) t 1 t 2 exor t 1 t 2 True True False False True True True False True False False False
Literals and definitions Values that are given literally: True False Can be used in function definitions: mynot :: Bool -> Bool mynot True = False mynot False = True exorʹ True x = not x exorʹ False x = x These definitions use pattern matching on the argument values
Property testing with QuickCheck not x == mynot x exor x y == exor x y exor x y == (x /= y)
Integer and Int > maxbound :: Int 9223372036854775807 > minbound :: Int -9223372036854775808 > maxbound :: Integer <interactive>:19:1: No instance for (Bounded Integer) arising from a use of maxbound In the expression: maxbound :: Integer In an equation for it : it = maxbound :: Integer
Negative literals: caution > negate -34 <interactive>:22:1: No instance for (Show (a0 -> a0)) arising from a use of print In a stmt of an interactive GHCi command: print it > negate (-34) 34
Operators + sum * product ^ raise to power - difference div whole number division mod remainder abs remove sign negate change sign
Relational operators > greater than (and not equal to) >= greater than or equal to == equal to /= not equal to <= less than or equal to < less than (and not equal to)
Overloading Using the same symbol or name for different operations e.g., == applies to Int, Integer, and Bool > :print == == = (_t3::eq a1 => a1 -> a1 -> Bool)
Survey: Monty Hall problem A. I ve not heard of the Monty Hall Problem B. I ve heard of it, but don t know the solution C. I know the solution to the Monty Hall Problem
Let s make a deal!
Monty Hall problem To maximize your chances of winning the big prize, you should A. Stick with the door you originally chose B. Switch to the other unrevealed door C. Randomly choose whether to switch doors https://en.wikipedia.org/wiki/monty_hall_problem
Guards (conditions) A Boolean expression to allow alternatives in function definitions. For example: max :: Integer -> Integer -> Integer max x y x >= y = x otherwise = y
Guards (conditions) A Boolean expression to allow alternatives in function definitions. For example: max :: Integer -> Integer -> Integer max x y x >= y = x True = y
Guards (conditions) Evaluated in order until the first guard that evaluates to True. foo x1 x2 xk g1 = e1 g2 = e2... otherwise = e First g1, then g2, etc.
Conditional expressions if condition then m else n For example: maxʹ :: Integer -> Integer -> Integer maxʹ x y = if x >= y then x else y
Property testing with QuickCheck max x y == maxʹ x y x <= max x y && y <= max x y x == max x y y == max x y What about the following? x == max x y `exor` y == max x y
Char: characters Literal characters are written inside single quotes: 'a',, 'z', 'A',..., 'Z', etc. Escape characters: '\t' tab '\n' newline '\\' backslash (\) '\'' single quote (') '\"' double quote (")
Character representation > fromenum 'A' 65 > '\66' 'B' These are the ASCII encodings of 'A' and 'B'. > minbound :: Char '\NUL' > '\0' '\NUL' > maxbound :: Char '\1114111' [Try the command man ascii on Linux or your Mac]
Conversions offset :: Int offset = fromenum 'A' fromenum 'a' toupper :: Char -> Char toupper ch = toenum (fromenum ch + offset) isdigit :: Char -> Bool isdigit ch = ('0' <= ch) && (ch <= '9')
String > "This is a string!" "This is a string!" > putstr "" > putstr "\n" > putstr "\99a\116\n" cat > "cat" ++ "fish" "catfish"
Strings and values > show (2+3) "5" > show (True False) "True" > read "True" :: Bool True > read "3" :: Int 3
Float: Floating point numbers Fixed precision to match hardware floating point representations: Float is 32 bits Double is 64 bits Represent subsets of the real numbers. Example literals: 0.31426-23.12 231.61e7-3.142e03
Operators
Operator Type Meaning + - * Float -> Float -> Float Add, subtract, multiply Operations on reals / Float -> Float -> Float Fractional division ^ Float -> Integer -> Float Exponentiation x^n = x n for n N ** Float -> Float -> Float Exponentiation x**y = x y == /= < > <= >= Float -> Float -> Bool Relational operators abs Float -> Float Absolute value acos asin atan Float -> Float Inverse of cosine, sine, and tangent ceiling floor round Float -> Integer Convert fraction to integer cos sin tan Float -> Float Cosine, sine, and tangent exp Float -> Float Powers of e frominteger Integer -> Float Convert from an Integer fromintegral Int -> Float Convert from an Int (or any integral value) log Float -> Float Logarithm to base e logbase Float -> Float -> Float Logarithm to arbitrary base as first argument negate Float -> Float Change sign pi Float The constant pi signum Float -> Float 1.0, 0.0, or -1.0 according +ve, zero, or ve sqrt Float -> Float (Positive) square root
Beware of the following Non-numerical results > 1 / 0 Infinity No automatic conversion from Integer to Float > (floor 5.6) + 6.7... > fromintegral (floor 5.6) + 6.7 11.7
Syntax: definitions and layout Layout separates definitions: offside rule ends at the first piece of text that lies at the same (or lower) indentation level A sequence of definitions should appear at the same indentation level. Alternatively, use ; to end the current definition: answer = 42; facsix = 720 Names: start with lower case for variables, functions: foo, bar, baz start with upper case for types, constructors (e.g., True), modules, etc. camel case for multi-word names: foobarbaz