Back
ExamFull examExam paper only

06 07 15 1

Full exam for Principles of Programming Languages in the Computer Engineering degree programme at Politecnico di Milano. The document covers: Principles of Programming Languages 2015.07.06 Notes • NAME: __________________________________ • Did you present a small project? YES / NO • Total available time: 2h. • Y ou may use any written material you need. • Y ou cannot use computers or phones during the exam. 1 Scheme

Principles of Programming LanguagesFull exam

Document information

What's included in this study material

Full exam for Principles of Programming Languages in the Computer Engineering degree programme at Politecnico di Milano. The document covers: Principles of Programming Languages 2015.07.06 Notes • NAME: __________________________________ • Did you present a small project? YES / NO • Total available time: 2h. • Y ou may use any written material you need. • Y ou cannot use computers or phones during the exam. 1 Scheme

Import quality: text was extracted directly from the original document.

Extracted content from the document

Representative passages recognised in different parts of the material. The full extracted text remains available to search, while this compact preview makes the page easier to read.

Page 1

Principles of Programming Languages 2015.07.06 Notes • NAME: __________________________________ • Did you present a small project? YES / NO • Total available time: 2h. • Y ou may use any written material you need. • Y ou cannot use computers or phones during the exam. 1 Scheme 1.1 Closures (6 points) Consider this code: (define (producer ag1 ag2) (let loop ((i 1)) (if (< i 10) (begin ((if (odd? i) ag1 ag2) i) (loop (+ i 1))) (cons (ag2 ’end) (ag1 ’end))))) Define two closuresclos1 and clos2 for passing them toproducer such that it returns the list’(20 9 7 5 3 1) (note that 20 = 2 + 4 + 6 + 8 ). 1.2 Macro (6 points) Define a macro multiple-apply, (multiple-apply (fun-1 fun-2 ...) to list-1 list-2 ...) where fun-i are functions and to is a keyword, which returns a list containing the result of applying fun-i to list-i. E.g. > (multiple-apply (+ - *) to ’(1 2 3) ’(3 4) ’(9 -2)) (6 -1 -18) 1 2 Haskell 2.1 Split (5 points) Assume this is a possible Haskell variant of producer in Exercise 1, in which the closures’ states are made explicit. producer ag1 ag2 = prod’ ag1 [] ag2 0 1 where prod’ ag1 st1 ag2 st2 i | i >= 10 = (st2:st1) prod’ ag1 st1 ag2 st2 i | odd i = prod’ ag1 (ag1 i st1) ag2 st2 (i+1) prod’ ag1 st1 ag2 st2 i | even i = prod’ ag1 st1 ag2 (ag2 i st2) (i+1) 1. Define two suitable functions for producer’s two arguments, such that its call returns [20,9,7,5,3,1]. 2. Write the type of prod’, assuming that all the numbers have type Int. 2.2 Duo-fold (6 points) Define an higher-order function called duofold, which takes two binary functions f and g, a starting value t and a (finite) list [e1, e2, . . .], and returns . . . f(g(f (t, e1), e2), e3), .... Please, write also its type. Example: duofold (+) (-) 0 [1,2,3,4] returns −2 (i.e. 0 + 1 − 2 + 3 − 4). 3 Prolog 3.1 Check…

Preview

First page of the document.

First page: 06 07 15 1