CA215 Languages and Computability

 

Functional Programming Lab 1: Getting Started

 

Aim

 

The aim of this practical is to show you how to use the Hugs system. This will involve becoming familiar with the Hugs environment, entering expressions into Hugs, loading scripts and creating your own scripts.

 

1. Starting Hugs

 

To start Hugs, simply type hugs at the Linux prompt.

 

2. A Hugs Session

 

You can now enter some expressions at the Hugs prompt and Hugs will evaluate them. Try the following examples. Many of them use functions provided in the standard prelude. Are the answers what you expected?

 

3

3.0

‘3’

“3”

3+4

$$ - 2

20 `div` 5

20 `mod` 5

20 / 5

20.0 / 5.0

5 / 0

2 ^ 5

sqrt 2.0

sqrt (-1)

sqrt 5*5

sqrt 15.0 >= 3.5

sin (pi/2.0)

6 < 4

3>4 && 4>3

3>4 || 4>3

not (3>4 && 4>3)

"Hello World!\n"

putStr "Hello World!\n"

“abc" ++ "def"

 

length “This sentence no verb.”

words “The quick brown fox jumped over the lazy dog”

“The length of \”this sentence\” is :” ++

show (length “this sentence”)

 

3. Loading a Haskell Script

 

Download the file itoa.hs. Don’t worry – you are not expected to be able to fully understand this program at this stage. The main function in the script is cvt which takes a positive integer argument and returns as result the English phrase for that number. For example: cvt 132 =“One hundred and thirty-two.”. You should also try: wordify 132.

           

To try out this script, it must be loaded into Hugs, so start a Hugs session if you don’t already have one running. Now you can load the script into Hugs by typing :load itoa.hs at the Hugs prompt.

 

4. Creating a Haskell Script

 

To create your own script, open the editor and type in the following:

 

-- CA215 Languages and Computability

--

-- <your name here> October 2005

 

cube :: Int -> Int

cube x  =  x * x * x

 

edge, volume :: Int

edge = 3

volume = cube edge

 

surfaceArea :: Float -> Float

surfaceArea r = 4.0 * pi * r^2

 

Save the file with an appropriate name e.g. exercise1.hs. Don’t forget that the suffix must be .hs or Hugs won’t know that it is a Haskell script. To try out the script that you have just created, it must be loaded into Hugs, so start a Hugs session if you don’t already have one running. Now you can load your script as described in the previous exercise. If you haven’t made any typing errors, Hugs should successfully load your script. If you have made some typing mistakes, Hugs will give you an error message while attempting to load the script. The error message will include the line number at which Hugs could not continue. An easy way to go to that line in an editor is to type :edit at the Hugs prompt. When you save your corrections, your script will automatically be re-loaded into Hugs. Note that your error will often precede the line reported in the error message. Alternatively, if you already have an editor running, you can make corrections there, and then tell Hugs to re-load by typing the command :reload, or just :r. When you have successfully loaded your script you should experiment by getting Hugs to evaluate a few expressions using the definitions in your script.