Haskell Assignment 1 Solution

$30.00 $24.90


You must use the literal haskell format (.lhs). You should submit ONE lhs file with all questions answered in the file. Name your file “Hw11.lhs”.

As usual, this file should be wrapped in a tar.gz directory with your bmail name e.g. “eway1_hw11.tar.gz”. Note that there are several questions herein, particularly those that end in “why?”. Put the answers to these questions in comments near any associated code.

1. [0pts]Using interactive Haskell.

A. GHCi can be opened on the terminal with

$ ghci

a. Opening ghci will give you a prompt like:

GHCi, version 8.0.2: http://www.haskell.org/ghc/ 😕 for help Prelude>

    1. Online documentation for the GHC: http://www.haskell.org/haskellwiki/GHC/GHCi http://www.haskell.org/ghc/docs/latest/html/users_guide/ http://www.haskell.org/hoogle/

    1. Online book for Haskell http://learnyouahaskell.com/

  1. Interactive commands are available at the prompt. For the most part, it doesn’t matter whether you’re using GHCi or Hugs.

The most useful will be:

Prelude> 😕 –List available commands.

Prelude> :l MyFile.lhs –Load a file. This supports tab-completion so you don’t have to type out the whole file name.

Prelude> :r –Reload the last loaded file, if you’ve edited it somewhere else.

Prelude> :e –Open a text editor. Not supported in Hugs.

Prelude> :!ls — :! runs shell commands

Prelude> :!pwd

Prelude> :cd CS471 –:!cd won’t work; you need to use :cd.

Prelude> :t factorial –Get the type of an expression.

C. At the prompt type any numerical expressions.

Prelude> 3 + 4

Prelude> sqrt(25)

Prelude> (+) 3 4 –Infix operator as prefix.

Prelude> div 10 3

Prelude> 10 `div` 3 –Prefix operator as infix.

Prelude> (-) 2 5

Prelude> (2 – ) 5 –Currying.

Alternation, types, set of types (Classes), type inference and specifying types


  1. [5pts]Create a Haskell script file using your favorite editor. We are going to create a program/script using the literal style of coding. Each line of code will begin with “> “. DO NOT MIX TABS AND SPACES. Lines with comments can begin with any character except “>”.

a. The first 2 lines of the file should be:

      • module Hw11

      • where

Notice there is a space between “>” and module, the identifer “Hw11” begins with a capital.

There can be 1 or more spaces between “>” and where.

  1. Line 3 should be a blank line. Enter the following comment on line 4. The comment can start in column 1.

Define factorial. Let Haskell infer the type of factorial.

  1. Enter another blank line. Type the following 1 line definition of factorial. Remember “=” means defined as and “==” is the equals operator. This code uses a if expression. What is the difference between an if statement and an if expression?

    • factorial n = if n == 0 then 1 else n * factorial (n – 1)

  1. Save text in a file named “Hw11.lhs”. Notice the name matches the module name.

  1. Make sure you are in the folder with “Hw11.lhs” and open Hugs or GHCi. At the prompt type:

….> :l Hw11.lhs

Notice the prompt will change to the name of the module


f. At the prompt type the expression

Hw11> factorial 5

What do you see? What is the inferred type of factorial? (At the prompt typed :t factorial).

  1. [0pts]Open “Hw11.lhs” in an editor. One way is to type “:e” at the prompt and an editor should open. The symbol/operator “::” means has type. Add the following definitions to the file (Lines 7 -10). (Line

    1. tells Haskell the type of the fact1,

  • fact1 :: Int -> Int

  • fact1 n = if n == 0 then 1 else n * fact1 (n – 1)

  • fact2 :: Integer -> Integer

  • fact2 n = if n == 0 then 1 else n * fact2 (n – 1)

Save and reload the script. (:r).

4. [5pts]Lets run some experiments. At the prompt type these expressions:

Hw11> factorial 12

Hw11> fact1 12

Hw11> fact2 12

Hw11> factorial 13

Hw11> fact1 13

Hw11> fact2 13

Hw11> factorial 500

Hw11> fact1 500

Hw11> fact2 500

What happens and why?

  1. [10pts]Lets runs some experiments. At this point you may not understand the inferred types. At the prompt type the following commands:

:t fact1

:t fact1 5

:t (*)

:t (==)

:t 5

:t 5.1

:t 5::Int

:t factorial

:t factorial 5

:t (-)

:t (2-)

:t (-) 2

:t error

:t (2 – ) –Currying.

“Num a” means — Num is a set of types and a belongs to the set. Examples of types that belong to Num are Int, Integer, Float as well as user defined types. Haskell calls these sets –CLASSES– At the prompt type the following expressions:

Hw11> factorial (-2)

What happens and why?

At the prompt type the following expressions:

Hw11> factorial -2

What happens?

  1. Edit “Hw11.lhs” again. Instead of using if expression, you will use pattern matching. Noticed that the first definition of factP has “0” instead of an variable. Add the following (lines 11-13):

    • factP :: Integer -> Integer

    • factP 0 = 1

    • factP n = n * factP(n -1)

Reload the module and test factP using data from part2 #3

  1. Edit “Hw11.lhs” again. Instead of using if expression, you will use guards. Notice the indentation (spaces between “>” the “|” (the guard)). In addition error will be used to catch negative input. Add the following (lines 14-17)

    • factG x

    • | x < 0= error “neg x”

> | x == 0 = 1

  • | otherwise = x*factG(x-1)

How many definitions are there? Reload Hw11 and test factG. What happens when you type factG (-2)?

  1. Edit “Hw11.lhs” again. Add the following two definitions (lines 18-27).

    • factG2 :: Integer -> Integer

    • factG2 n

    • | n < 0= error “neg n”

> | n == 0 = 1

  • | otherwise = n * factG2 (n – 1)

  • factE :: Integer -> Integer

  • factE n

  • | n < 0= error “neg n”

> | n == 0 = 1

  • | otherwise = n * factE n – 1

At the prompt type these expressions:

Hw11> factorial 5.1

Hw11> factG 5.1

Hw11> factG2 5.1

What happens and why?

At the prompt type these expressions:

Hw11> factG2 5

Hw11> factE 5

What happens? Try to guess why factE behaves this way?