Programming Languages (CSC-302 98S)
Outline of Class 27: Lazy Functional Programming in Haskell
Held: Wednesday, April 8, 1998
- Any question on assignment
- Reminder: there's a cool talk during the math journal club tomorrow.
- Today we'll be moving from Scheme to Haskell so as to explore other
issues in functional programming.
- I'll admit that I do most of my Haskell programming on the Mac, so
I may rely on the exotic programming languages folks for help running
Haskell on our HPs.
- We had a short assignment for today: what is the value of the
following Scheme expression and why?
(let ((a 2))
(let ((a 4)) a)
- Similarly, what's the value of the second of these two expressions
> (define eh (let ((a 5)) (lambda () a)))
> (let ((a 2)) (eh))
- Haskell is a functional language that provides a somewhat different
perspective on functional programming than Scheme.
- Haskell is typed (with an automatic type inference system).
- Haskell is lazy rather than eager (it delays evaluation of expressions
"as long as possible"). We'll return to this concept soon.
- Haskell is pure: functions don't have side effects.
We also say that Haskell functions are referentially
transparent: the value they return depends only on the
arguments they are sent.
- All functions in Haskell are curried.
- Haskell has a different syntax (whoopdedoo)
- The core Haskell web page is at
Gentle Introduction to Haskell at
is a good place to start learning the stuff that I don't teach you.
- Like many functional languages, Haskell has a relatively simple
- Function application is notated by the function, a space, and
its argument. For example,
f a represents
"apply f to a".
- No parentheses are necessary (except for clarity or to circumvent
- Since Haskell is Curried, the apply operation (a space) is
left-associative. For example,
f a b is
"apply f to a; apply the resulting function to b".
- To get the alternate interpretation (apply a to b, then apply
f to the result), one would write
f (a b).
- Mathematical operations can be written in infix notation, as
a + b.
- Haskell uses capitalization to indicate the roles of various
program components. In Haskell, these capitalization standards
are requirements rather than recommendations.
- Functions, variables, and type variables begin with a lowercase
- Types and type constructor begin with an uppercase letter.
- Data types are described with the
data keyword, using
data Typename = Definition
- Functions are described by giving the function name, any arguments,
an equals sign, and the body. For example,
fact n = if (n == 0) then 1 else n * (fact (n - 1)
- You can also describe the types of things (e.g., functions), two
colons, and a type expression. For example,
fact :: Int -> Int
- List are written with square brackets and commas between the elements.
All the elements must be the same type. For example,
[4, 2, 4, 1]
- Vectors (also records and products) are written with
parentheses and commas between the elemnts.