Lecture overview -- Keyboard shortcut: 'u'  Previous page: Static type checking -- Keyboard shortcut: 'p'  Next page: Types in functional programming languages -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home    Lecture 2 - Page 15 : 46
Functional Programming in Scheme
Expressions, Types, and Functions
An example of type checking

Is the expression   (+ 1 (if (even? x) 5 "five"))   correct with respect to types?
The example shows an arithmetic expression that will cause a type error with most type checkers. However, if x is even the sum can be evaluated to 6. If x is odd, we encounter a type error because we cannot add the integer 1 to the string "five".

  • Weak typing

    • It is not realized that the expression (+ 1 "five") is illegal.

    • We can imagine that it returns the erroneous value 47

  • Strong typing

    • If, for instance, x is 2, the expression (+ 1 (if (even? x) 5 "five")) is OK, and has the value 6

    • If x is odd, it is necessary to identify this as a problem which must be reported before an evaluation of the expression is attempted

  • Static typing

    • (+ 1 (if (even x) 5 "five"))   fails to pass the type check, because the type of the expression cannot be statically determined

    • Static type checking is rather conservative