You are on page 1of 3

Introduction to Programming : Assignment 

Due: November , .  pm

Important Instructions: Submit your solution in a single file named loginid.3.hs on Moodle. For
example, if I were to submit a solution, the file would be called spsuresh.3.hs. You may define
auxiliary functions in the same file, but the solutions should have the function names specified
by the problems.

1. Consider the Fibonacci words defined as follows:

fibWord :: Integer -> [Char]


fibWord 0 = "a"
fibWord 1 = "b"
fibWord n = fibWord (n-2) ++ fibWord (n-1)

The sequence of fibWords start out as "a", "b", "ab", "bab", "abbab", .... Write a pro-
gram isFibWord :: [Char] -> Bool which returns True if the input string is a Fibonacci
word, and returns False otherwise. Sample cases:

isFibWord "abbab" = True


isFibWord "bababbababbabbababbab" = True
isFibWord "ababababababababababa" = False

2. Every irrational number can be written as an infinite continued fraction (and√every ra-
1+ 5
tional as a finite continued fraction). For example, the golden ratio ϕ = can be
2
written as1
1
1+
1 + 1+ 1 1
1+...

This is represented more succinctly as [1; 1, 1, 1, 1, . . .]. (Note the semicolon after the first
entry.) If we truncate this list at some finite point, we get a finite rational approximation
for ϕ. Rational numbers are represented using the data type Rational in Haskell. The ratio
p/q is represented using the % operator defined in Data.Ratio. Acquaint yourself with the
other functions defined in Data.Ratio, like numerator and denominator, and the function
fromIntegral.
Assume the following Haskell definitions:
1
1
This can be verified by denoting the continued fraction as x and observing that x = 1 + , i.e. x2 = x + 1, and
x
solving for x.
phi :: Double
phi = (1 + sqrt 5) / 2

computeFrac :: Rational -> Double


computeFrac x = fromIntegral (numerator x) / fromIntegral (denominator x)

Write a function approxGR :: Double -> Rational which returns a close rational approxi-
mation for the golden ratio, i.e. it accepts an input epsilon and returns some r :: Rational
such that abs (phi - computeFrac r) < epsilon.
Note: Do not use the approxRatio function from Data.Ratio.
Sample cases: (Since there are multiple answers possible, the cases below are only indica-
tive. We will check the correctness of your solution by actually calculating if the error is
less than epsilon.)

approxGR 0.0001 = 144 % 89


approxGR 0.0000000000001 = 3524578 % 2178309

3. We define a binary tree like this:

data Tree = Empty | Fork Tree Int Tree

Imagine that the tree has a coin on each of its vertices, given by the corresponding value
at that node. All the coins are assumed to have a non-negative value.
A monkey is trying to climb down a (finite) Tree, starting from the root. Define the function
maxCoinsDescend :: Tree -> Int, which computes the maximum value of coins it could
collect along a path starting from the root.
What if it were to just traverse any path in the tree that does not repeat vertices (not neces-
sarily from root to leaf)? Define maxCoinsPath :: Tree -> Int, computing the maximum
number of coins it could collect along any path.
Sample cases:

maxCoinsDescend Empty = 0
maxCoinsDescend (Fork Empty 10 Empty) = 10
maxCoinsDescend (Fork Empty 10 (Fork Empty 20 (Fork Empty 30 Empty))) = 60
maxCoinsDescend (Fork (Fork Empty 20 Empty) 10 (Fork Empty 30 Empty)) = 40

maxCoinsPath Empty = 0
maxCoinsPath (Fork Empty 10 Empty) = 10
maxCoinsPath (Fork Empty 10 (Fork Empty 20 (Fork Empty 30 Empty))) = 60
maxCoinsPath (Fork (Fork Empty 20 Empty) 10 (Fork Empty 30 Empty)) = 60

2
maxCoinsPath $
Fork (Fork Empty 10 Empty)
10
(Fork (Fork Empty 30 Empty) 5 (Fork Empty 30 Empty))
= 65

4. Create an infinite tree funTree :: Tree such that at level n of the tree, all nodes hold the
value n.
Define chopOff :: Int -> Tree -> Tree, that chops off all but the first n+1 layers from the
root. Your chopOff should terminate on an infinite tree as well.
Sample cases:

chopOff 0 funTree = Fork Empty 1 Empty


chopOff 1 funTree = Fork (Fork Empty 2 Empty) 1 (Fork Empty 2 Empty)

You might also like