Download Haskell

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Addition wikipedia , lookup

Big O notation wikipedia , lookup

Arithmetic wikipedia , lookup

Principia Mathematica wikipedia , lookup

Non-standard calculus wikipedia , lookup

Function (mathematics) wikipedia , lookup

History of the function concept wikipedia , lookup

Factorial wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
Introduction to Programming I
Mid-Semester Examination, Semester I
Date:29 September 2006
Duration: 3 hours
Marks: 30
Weightage : 20 % Note: Whenever a question asks for a
Haskell function, write down the most general type of the function before the
actual function definition, including any dependency on type classes.
1. Define the following functions in Haskell.
(a) applyEach, that takes a list of functions [f 1 , f 2 , . . . , f n ] and
applies each of them to a given value v, returning the list of
values [f 1 (v), f 2 (v), . . . , f n (v)]. For instance, assuming factorial
is already defined:
textbfapplyEach [(+3),factorial]5 ) ⇒ [8, 120].
(b) applyAll that takes a list of functions [f 1 , f 2 , . . . , f n ] and a value
v and returns the value f 1 (f 2 (. . . (f n (v)) . . .)). For instance:
applyEach [(+3),factorial]5 )⇒ 123.
(4 Marks)
2. Define a Haskell function remdup to remove all duplicated values from
an input list.Analyze the time complexity of your function
(4 marks)
3. Consider the following naive definition of foldl
foldl :: (b→ a→ b) → b → [a]→b
foldl f x [] = x
foldl f x l =f (foldl f x (init l)) (last l)
(a) What is the time complexity of this definition of foldl
(b) Write an alternative definition that works in time O(n),where n
is the length of the list to be folded
(5marks)
4. The built-in function concat dissolves one level of brackets in a list.
Suppose we want to extend concat to a function flatten that dissolves
all but the outermost level of brackets in a list. Here are some examples
of how flatten should work:
• flatten [1, 2, 3, 4, 5] = [1, 2, 3, 4, 5]
1
• flatten [[1, 2], [], [3, 4, 5]] = [1, 2, 3, 4, 5] =concat [[1, 2], [], [3,
4, 5]]
• flatten [[[], [1, 2]], [[]], [[3, 4], [5]]] = [1, 2, 3, 4, 5] =concat
(concat [[[], [1, 2]], [[]], [[3, 4], [5]]])
As the third example suggests flatten can be thought of as a repeated
application of concat.
Is it possible to define flatten in Haskell? Explain your answer.
marks)
(3
5. To build up statistics about library usage,the CMI office would like to
complie a list of students who have issued atleast one book during the
semester.Each student is assigned
a unique roll number(of typeInt and each book is assigned a unique
accesion mumber(of type String).Information about book issues is
maintained in terms of and accesion numbers and roll numbers.
Write a haskell function to extract the information required by the CMI
office.
The input to your function consists of 2 lists.
• The first line has issue information for the current semester about
all the books in the library.Each item is the list is a pair AccNo,ListOfStudents)
of the type (String,[Int]),where ListOfStudents contains the roll
numbers of all students who have issued the book with accesion
number AccNo this semester
• The second list maps roll numbers to names of students. It is
a list of pairs /emph(Name,RollNumber) where each pair of type
(String,Int) specifies the name of a student and the corresponding
roll number
The output of your function should be a list consisting of the
names of the students whohave issued at least one bookduring
the current semester. This list need not be in any specific order
but it should not have any duplicate entries. You may assume
that no 2 students have the same name.
(6 marks)
6. We consider infix arithmetic expressions over integers using the operators
+,-,* without parentheses and without any assumptions about the
order in which to evaluate subexpressions. Thus an expression such as
6*3+2 may be evaluated as (6*3)+2 = 20 or 6*(3+2) = 30,depending
on the order of evaluation
2
The order of evaluation of an expression can be unambigously described
using a tree to describe its structure.Consider the following data declaration
in Haskell:
data Expr = Value Int| Add Expr Expr|Sub Expr Expr|Mult Expr Expr
Write the following functions in Haskell:
(a) evalExpr:: Expr → Int
that takes as input an Expr and evaluates it
(b) parseExpr :: String→[Expr]
that takes as input an arithmetic expression without parantheses
in the form of a String (for example,6*3+2) and generates as
output a list of all valid Expr trees for this expression
(c) evalparseExpr::String →[Int]
that takes as input an arithmetic expression without parantheses
in the form of a String and generates as output a list of all valid
evaluations for this expression without duplicates.
In (b) and (c),you may assume that each integer that appears in the
input expression is a single digit positive integer.You may also assume
that the input expression is legal - that is, it is guaranteed to correspond
to atleast one Expr tree
(8 marks)
3