CSc 372 - Comparative Programming Languages
3 : Haskell -- Introduction
Christian Collberg
Department of Computer Science
University of Arizona
- Haskell is a functional programming language.
- We study Haskell because, compared to other functional languages
- Haskell is statically typed (the signature of all functions and the
types of all variables are known prior to execution);
- Haskell uses lazy rather than eager evaluation
(expressions are only evaluated when needed);
- Haskell uses type inference to assign types to
expressions, freeing the programmer from having to
give explicit types;
- Haskell is pure (it has no side-effects).
- Haskell implementations are also
interactive which means that the user interface is
like a calculator; you enter
expressions, the Haskell interpreter checks them,
evaluates them, and prints the result.
This is called the ``read-eval-print'' loop:
ReadEvalPrint
eliza = interact (writeStr hi $ session initial [])
where hi = "\n\
\Hi! I'm Eliza. I am your personal therapy computer.\n\
\Please tell me your problem.\n\
\\n"
session rs prev
= readLine "> " (\l ->
let ws = words (trim l)
(response,rs') = if prev==ws then repeated rs else answer rs ws
in writeStr (response ++ "\n\n") $ session rs' ws)
- Real functional programs are, naturally, a bit
more complex. They make heavy use of
- higher-order functions, functions
which take functions as arguments.
- function composition,
which is a way to combine simple functions
into more powerful ones.
- function libraries, collections of
functions that have proven useful.
The standard.prelude that you've
seen that the Haskell interpreter loads
on start-up, is one such collection.
- So what does a ``real'' functional Haskell program look like?
Let's have a quick look at one simple (?) function,
commaint.
- commaint works on strings, which are simply
lists of characters.
- You are not supposed to understand this! Yet...
From the commaint documentation:
[commaint] takes a single string argument
containing a sequence of digits, and outputs the
same sequence with commas inserted after every
group of three digits,
Sample interaction:
commaint in Haskell:
CommaInt
commaint in Haskell:
commaint in English:
``First reverse the input string. Take the resulting string and
separate into chunks of length 3. Then append the chunks
together, inserting a comma between chunks.
Reverse the resulting string.''
- group n is a ``local function.'' It takes a string and
an integer as arguments. It divides the string up in
chunks of length n.
- reverse reverses the order of the characters in a string.
- drop n xs returns the string that remains when the first
n characters of xs are removed.
- iterate (drop 3) s returns the infinite (!) list of strings
- take n s returns the first n characters of s.
- map (take n) s takes a list of strings as input. It
returns another list of strings, where each string has been
shortened to n characters. (take n) is a
function argument to map.
- takeWhile (not.null) removes all empty strings from
a list of strings.
- foldr1 (
x y->x++","++y) s takes a list
of strings s as input. It appends the strings
together, inserting a comma inbetween each pair of strings.
- Since Haskell is an interactive language,
we can always try out (parts of) functions
that we don't understand.
Christian S. Collberg
2007-08-21