ICS 313: Fundamentals of Programming Languages (991)                                                       Section: 03

 

Date:   8th December 1999                                   Quiz #4                                                   Time: 20 Minutes

 

Question 1:

Answer only two of the following parts                                                                                         (6 points)

i)            Define only one of the following functional forms: composition, construction or apply-to-all. Give an example of the form you choose.

Composition (f ° g) takes two function as input and produces an answer obtianed from applying the first function (f) to the result of applying the second function (g).

Example: Assume that:    f(x) = x+2        g(x) = 3 * x      then f ° g = f(g(x)) produces (3*x) + 2

Construction ([f, g]) takes a list of functions as parametrs. When applied to an argument, a construction applies each of its parametrs to the argument and collects a list of result.

Example: Assume that:    f(x) = x+2        g(x) = 3 * x      then [f, g] (4) produces [6, 12]

Apply-to-all (µ) takes a single function as a parametr. When applied to a list of arguments, apply-to-all applies its functional parametr to each argument in the list and collects a list of result.

Example: Assume that:    f(x) = x+2        then  µ (f, [2,3,4]) produces [4, 5, 6]

ii)          What is a lambda expression? Give an example.

A lambda expression is a mthematical expression that specifies the parametr(s) and the mapping of a function without a name. The value of the lambda expression is the function itself.

For example, l(x) x3 + 5 x2 + 2 x

iii)         Breifly state your understanding of the difference between functional and imperative programming paradigms.

In imperative paradigm the programmer writes progrma stating how the task need to be done and it is heavily based on assignment. In Functional paradigm, on the other hand, the programmer writes progrma stating what are the task that need to be done and it is based on mathematical functions.

 

 

Question 2:

a)   Given the following Haskell program:                                                                                       (4 points)

 

 

fun :: Int -> [Int]

fun m = [i | i<- [1..m]; m ‘mod’ i == 0]

 

 

i)                       What does this program do?

This program takes an integer parameter m and produces a list of all its factors, integers that are divisible by m.

 

ii)                      What will  fun 6  produce?

fun 6  produces the list [1, 2, 3, 6]

 

 

b)   Write a Haskell function insert that takes an intger n and a sorted list of intgers L and inserts n in the right place in L to keep it sorted. .:                                                                                                               (5 points)

 

Solution 1

Solution 2

Solution 3

insert n [ ]     = [n]

insert n [ ]      = [n]

insert n [ ]    = [n]

insert n (h:t)  = if n < h    then [n]++(h:t)

insert n (h:t)  | n<h       = [n] ++ (h:t)

insert n lst   =

                                           else [h]++insert n t

                        | n>h       = [h] ++ insert n t

[l | l<- lst, l<n] ++ [n] ++[k | k<-lst, k>a]