Exercise 1
Part 1
- Expression and variable must have compatible types.
- Disadvantages: types help detect programming error; helps with program design
Part 2
ghci> "CS2" ++ "PLD"
"CS2PLD"
-- [Char]
ghci> True || False
True
-- Boolean
ghci> 42*42
1764
-- Num a => a
ghci> sqrt 2
1.4142135623730951
-- Floating a => aExercise 2
Part 1
- Refer to slides; 4 types
- Efficiency; no time needed for allocation / deallocation; address is set during compile time
Part 2
We don’t know how many times we are going to recurse so we don’t know how much memory we need to use yet.
Exercise 3
- Refer to slides; 2 types; static -> variables defined before execution; dynamic -> depends on the calling sequence
- Dynamic is lenient but gets messy
Exercise 4
Part 1
Static: 5 Dynamic: 10
Part 2
global
g
global
g
# UnboundLocalError: local variable 'x' referenced before assignment
Exercise 5
let x = 3
f z = z + x in let x = True in f 6
-- output: 9
-- let x = True defined for the scope of "f 6"
-- but f has already captured xlet x = 3; f z = z + x in f 6
-- output: 9
-- same output as this removes a redundant instructionWith dynamic scope:
- first will error as Haskell tries to add a boolean to a number
- second will be 9
Exercise 6
Static; skipped Q.
Exercise 7
- Changing the flow of execution / jump to different part of the program
- Default initialise primitive type values. Do not initialise any local variables but allow them to be referenced. C does not allow jumps to the middle of a program.