| CS 331 Spring 2009 > Additional Lecture Notes for Wednesday, January 28, 2009 |
The following material was covered in class, in addition to the material from the text.
Later in the semester, we will be looking at the programming language Haskell (named after logician Haskell Curry). You will need to get access to some form of the Haskell language. Some options:
Please avoid “Helium”, due to some incompatibilities.
I have asked for Hugs to be installed in the Chapman 103 lab.
Hugs is a semi-integrated interactive system. It does not have its own editor; you may need to configure it. At the Linux prompt, if you run Hugs by typing
then it will use the editor you specified. I generally use gvim, and so I would typehugs -EEDITOR_NAME
Under Windows or MacOS ... figure it out.hugs -Egvim
At the Hugs prompt, if you type something starting with a colon (“:”), it will be taken as a Hugs command. Anything else is a Haskell expression. Type “:?” for a list of commands; these can be abbreviated.
To edit a file, type
or abbreviate “:edit” to “:e”. Names of Haskell source files traditionally end with “.hs” So, for example, you could type:edit FILENAME
:e test1.hs
Once you have written some code in your editor and saved it (if possible, keep the editor running), you load it into Hugs with “:load”:
Then you can type expressions involving things defined in your source file.:l test1.hs
If you change the code in the file, you can redo the last load command using “:reload”.
:r
Here is some Haskell code similar to that written in class. It defines a function fibo, which takes an integer parameter n and returns the nth Fibonacci number.
-- fibo: compute a Fibonacci number
fibo n = fst (fibox n)
where fibox 0 = (0, 1)
fibox n = (b, a+b)
where (a, b) = fibox (n-1)
This only defines a function, and so loading it into Hugs will not appear to do anything.
However, after you load it, you can use “fibo” in expressions. For example, if you type
at the Hugs prompt, you should get “55” as output. Here are some other things to try typing.fibo 10
Some observations.fibo 1000 fibo 1000000 [fibo n | n <- [1..10]]
We will do more with Haskell later in the class. If you want to learn before then, there are a number of good tutorials on the Web.
| CS 331 Spring 2009: Additional Lecture Notes for Wednesday, January 28, 2009 / Updated: 28 Jan 2009 / Glenn G. Chappell / ffggc@uaf.edu |
|