Basics Haskell is an example of a functional programming language. Some features of a functional language are: 1. No variables 2. Program = a list of function, type, and constant declarations 3. Functions can be called or treated like data A good reference on Haskell is: "Haskell: The Craft of Functional Programming" by Simon Thompson Addison-Wesley 1996. Of course there are plenty of tutorials avaialable on the web. Gofer is a free PC implementation of a Haskell-like language. Hugs is a free PC implementation of a subset of Haskell. Students should download and install Hugs. In these notes all subsequent references to Haskell will actually be references to Hugs. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Here is a complete list of the meta commands understood by the Hugs interpreter: :load load modules from specified files :load clear all files except prelude :also read additional modules :reload repeat last load command :project use project file :edit edit file :edit edit last module :module set module for evaluating expressions evaluate expression :type print type of expression :? display this list of commands :set set command line options :set help on command line options :names [pat] list names currently in scope :info describe named objects :browse browse names defined in :find edit module containing definition of name :!command shell escape :cd dir change directory :gc force garbage collection :version print Hugs version :quit exit Hugs interpreter ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Defining Functions and Constants A Haskell (Hugs) program is stored in one or more files called scripts. Scripts with a .lhs extension are called literate scripts. In a literate script all lines are assumed to be comments unless they are preceeded by the '>' symbol. The basic syntax for declaring a constant is: const = exp Here are a few examples: > pie = 3.1416 > val = 6 * 7 The basic syntax for defining a function is: fun param param ... = exp Here are a few examples of function definitions: > square x = x * x > area r = pie * square(r) ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Haskell (Hugs) types include: Int, Float, Char, Bool, String If A and B are types, then so are: A->B maps from A to B (A, B) tuples of A and B [A] lists of A There are two types of type declarations: type name = tp data name = Constructor tp1 ... tpn | ... Here are a few examples: > type Temperature = Float > type Vec3D = (Float, Float, Float) > data Seasons = Fall | Spring | Summer | Winter We can declare the types of the functions and constants we declare using the syntax: name:: Type Here are a few examples: > answer:: Bool > answer = True > origin:: Vec3D > origin = (0, 0, 0) > cube:: Float -> Float > cube x = x * x * x This is optional, because Haskell will infer the type, anyway. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > double x = x + x