Back to Logic Programming (3). On to Haskell (2).

Held: Monday, April 24, 2006

Summary: Today we begin our exploration of Haskell, a typeful, lazy, pure functional programming language.

Related Pages:

Notes:

• Reading to be distributed this afternoon.
• Presentation proposals due Wednesday.
• Desired form for final?

Overview:

• 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 `http://haskell.org/`. The Gentle Introduction to Haskell at `http://haskell.org/tutorial/index.html` is a good place to start learning the stuff that I don't teach you.
• In the MathLAN, you can use `ghci` to run Haskell.
• Haskell was developed by committee to provide a unified platform for the lazy functional programming community.
• Too much work seemed to be based on one particular subparadigm.
• Some overhead in converting syntaxes.
• The best lazy language at the time was commercial.
• ...

• Like many functional languages, Haskell has a relatively simple syntax.
• 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 precedence rules).
• 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 in `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 letter.
• Types and type constructor begin with an uppercase letter.
• Data types are described with the `data` keyword, using the form
```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 elements.

• One of the nicest things about Haskell is that it is pattern-based.
• In the equations you use to define functions, you can give patterns for the arguments.
• You can also use multiple equations to define one function.
• The patterns use variables and constructors.
• For example, to define factorial, we might instead use
```factorial :: Int -> Int
factorial 0 = 1
factorial n = n * (fact (n-1))
```
• Haskell attempts to apply the patterns in order. Some other functional languages require that the patterns not overlap (that is, that there be no expression for which both patterns apply).
• Prolog permits overlap, but tries both (via backtracking). Haskell uses the first match.
• Patterns are very powerful. They can even be used to define control structures, like `if` (I'm writing it as iif so that we don't collide with the built-in version.)
```iif :: Bool -> a -> a -> a
iif True x y = x
iif False x y = y
```
• Of course, we do need lazy evaluation to take advantage of this technique.

Back to Logic Programming (3). On to Haskell (2).

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Wed May 10 09:03:04 2006.
The source to the document was last modified on Thu Jan 12 09:00:38 2006.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS302/2006S/Outlines/outline.34.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu