H. Conrad Cunningham
1 September 2018
Copyright (C) 2017, 2018, H. Conrad Cunningham
Acknowledgements: I created these slides in Fall 2018 to accompany Chapter 4, First Haskell Programs, of the book Exploring Languages with Interpreters and Functional Programming.
Browser Advisory: The HTML version of this document may require use of a browser that supports the display of MathML. A good choice as of August 2018 is a recent version of Firefox from Mozilla.
Examine syntax and semantics of Haskell functions using factorial examples
Explore how to execute functions using Haskell REPL ghci
Factorial using product operator:
Factorial using recursive definition (or recurrence relation):
First line type signature of form object :: type.
Type name begins with uppercase letter
Type fact1 is function (->)
Int) argument, returns integerNo builtin natural number type
Second line defining equation fname parms = body
')Body expression if condition then expression1 else expression2
Bool) value, either True or FalseTrueFalseIndentation is significant, indicates nesting
then clause base case n == 0
else clause recursive case
fact1 to entire expression (n-1) – parenthesis0Difference with mathematical definition?
Function application by listing argument expressions after function name
f to argument expressions x and yf x yConventional infix form for convenience (syntactic sugar)
add x y written x + yFunction application higher precedence than others
f x + y same as (f x) + y| denotes guarded equation
TrueFunction fact2 equivalent to fact1
Two equations, each with a different pattern in parameter list
0 in first leg matches value 0n in second matches anythingFunctions fact1, fact2, and fact3 equivalent
To stop “infinite loop”, remove negative integers from domain
Function fact4 undefined for negatives – fails quickly
    fact4' :: Int -> Int 
    fact4' n 
        | n == 0    =  1 
        | n >= 1    =  n * fact4' (n-1)
        | otherwise = error "fact4' called with negative argument"Function fact4'
[1..n] generates list of integers from 1 to n, inclusive
Library function product multiplies elements of list
What if apply fact5 to negative?
[1..0] generates empty listproduct of empty list is 1 – identity for multiplicationfact5 (-1) yields 1Haskell factorial functions Factorial.hs
Haskell test script TestFactorial.hs
Discussed in Chapter 12 on software testing
See the Glasgow Haskell Compiler Users Guide
Start REPL
Load module Factorial holding factorial function definitions – assumes file Factorial.hs in current directory – load command abbreviated :l
Inquire about type of fact1
Apply function fact1 to 7, 0, 20, 1 – note 21 exceeds Int range
Apply functions fact2, fact3, fact4, fact5 to 7
Apply functions fact1, fact2, fact3 to -1 – infinite recursion – eventually runtime stack overflow
Apply functions fact4, fact4' to -1 – quickly return with error
Apply function fact5 to -1 – returns 1 because defined for negative integers
Set +s option to get information about time and space required – +t option to get type of the returned value
Exit GHCi
To edit source from within GHCi (using e.g. Aquamacs)
EDITORexport EDITOR=Aquamacs:set editor Aquamacs:edit command invokes editorHaskell has a syntax and semantics similar to mathematical notation
GHCI REPL enables interactive execution and exploration of Haskell functions
Haskell factorial functions Factorial.hs
Haskell test script TestFactorial.hs
Discussed in Chapter 12 on software testing