CS251 Jeopardy Spring 2005 CS251 Jeopardy Spring05 p.1/42 - - PowerPoint PPT Presentation

cs251 jeopardy
SMART_READER_LITE
LIVE PREVIEW

CS251 Jeopardy Spring 2005 CS251 Jeopardy Spring05 p.1/42 - - PowerPoint PPT Presentation

CS251 Jeopardy Spring 2005 CS251 Jeopardy Spring05 p.1/42 Gameboard Data Naming Laziness Xforms Imperative Control Types Potpourri 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5


slide-1
SLIDE 1

CS251 Jeopardy

Spring 2005

CS251 Jeopardy Spring’05 – p.1/42

slide-2
SLIDE 2

Gameboard

Data Naming Laziness Xforms Imperative Control Types Potpourri

1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5

CS251 Jeopardy Spring’05 – p.2/42

slide-3
SLIDE 3

Data 1

What data structure is commonly used in interpreters to associate names with values? Back

CS251 Jeopardy Spring’05 – p.3/42

slide-4
SLIDE 4

Data 2

What feature in OCAML, JAVA, and SCHEME, is responsible for reclaiming storage used by values that are no longer accessible from the program? Back

CS251 Jeopardy Spring’05 – p.4/42

slide-5
SLIDE 5

Data 3

How are “sum-of-product” data structures expressed in (i)

OCAML and (ii) JAVA?

Back

CS251 Jeopardy Spring’05 – p.5/42

slide-6
SLIDE 6

Data 4

What is the value of the following OCAML program? let yourMom = [[1;2]; [3;4;5;6;7]; [8]] in map (foldr (fun _ x -> 1+x) 0) yourMom Back

CS251 Jeopardy Spring’05 – p.6/42

slide-7
SLIDE 7

Data 5

Answer both of the following: (1) what problem does in- voking the following C function lead to and (2) how can the problem be fixed? int* nums (int n) { int a[n]; for (n = n-1; n >= 0; n--) { a[n] = n;

} return a; }

Back

CS251 Jeopardy Spring’05 – p.7/42

slide-8
SLIDE 8

Naming 1

List all of the free variables of the following HOFL expres- sion: (fun (a) (a b (fun (b) (+ b c)))) Back

CS251 Jeopardy Spring’05 – p.8/42

slide-9
SLIDE 9

Naming 2

List all of the following languages that are block structured:

PASCAL C JAVA OCAML SCHEME

Back

CS251 Jeopardy Spring’05 – p.9/42

slide-10
SLIDE 10

Naming 3

The following Common Lisp program denotes the factorial function, but a SCHEME program written in the same way would not. What language property accounts for the differ- ence in which the program is treated in the two languages? (defun fact (fact) (if (= fact 0) 1 (* fact (fact (- fact 1))))) Back

CS251 Jeopardy Spring’05 – p.10/42

slide-11
SLIDE 11

Naming 4

Give the value of the following expression in both statically scoped and dynamically scoped versions of SCHEME: (let ((a 1) (b 2)) (let ((f (let ((a 10)) (lambda () (+ a b))))) (let ((b 20)) (f)))) Back

CS251 Jeopardy Spring’05 – p.11/42

slide-12
SLIDE 12

Naming 5

Give the value of the following HOILIC expression under all four parameter passing mechanisms: call-by-value, call-by-reference, call-by-name, and call-by-lazy. Assume

  • perands are evaluated in left-to-right order.

(bind a 1 (bind b a (bind c (seq (<- a (* a 2)) a) (seq (<- b 10) (+ a (+ c c)))))) Back

CS251 Jeopardy Spring’05 – p.12/42

slide-13
SLIDE 13

Laziness 1

Which one of the following does not belong: lazy data call-by-value memoization call-by-need. Back

CS251 Jeopardy Spring’05 – p.13/42

slide-14
SLIDE 14

Laziness 2

In his paper “Why Functional Programming Matters”, John Hughes argues that laziness is important because it enhances something. What? Back

CS251 Jeopardy Spring’05 – p.14/42

slide-15
SLIDE 15

Laziness 3

Below are two definitions of an if0 construct: the first defined by desugaring, the second defined as a function: (1) (if0 Enum Ezero)

❀ (if (= Enum 0) Ezero Enum) (2) (def (if0 num zero) (if (= num 0) zero num)))

For (1) HOFL and (2) HOILIC, list all of the following parameter-passing mechanisms under which the two definitions are equivalent: call-by-value call-by-name call-by-lazy Back

CS251 Jeopardy Spring’05 – p.15/42

slide-16
SLIDE 16

Laziness 4

What are the elements of the list returned by evaluating the following HASKELL expression? take 5 (scanl (+) 0 ns) where ns = 1 : (map (2 +) ns) Back

CS251 Jeopardy Spring’05 – p.16/42

slide-17
SLIDE 17

Laziness 5

What is the value of the following statically-scoped call-by-value SCHEME expression? Assume left-to-right

  • perand evaluation.

(let ((n 0)) (let ((add! (lambda (x) (begin (set! n (+ n x)) n)))) (let ((add1 (lambda () (inc! 1))) (add2 (delay (inc! 2)))) (+ (* (add1) (force add2)) (* (add1) (force add2))))))

Extra: : What if the operand evaluation order is right-to-left?

Back

CS251 Jeopardy Spring’05 – p.17/42

slide-18
SLIDE 18

Xforms 1

What common program transformation have we studied that Alan Perlis once quipped could cause “cancer of the semi-colon”? Back

CS251 Jeopardy Spring’05 – p.18/42

slide-19
SLIDE 19

Xforms 2

Consider the following program transformation: (+ E E) => (* 2 E) For each of the following programming paradigms, indicate whether the above transformation is safe - that is, it preserves the meaning of the expression for all possible expressions E. purely functional imperative

  • bject-oriented

Back

CS251 Jeopardy Spring’05 – p.19/42

slide-20
SLIDE 20

Xforms 3

Consider the following HOILIC transformation: ((lambda (x) 3) E) => 3 List all of the following parameter passing mechanisms for which the above transformation is safe - that is, it preserves the meaning of the expression for all possible expressions

E.

call-by-value call-by-reference call-by-name call-by-lazy Back

CS251 Jeopardy Spring’05 – p.20/42

slide-21
SLIDE 21

Xforms 4

In SCHEME, the special form (or E1 E2) first evaluates

E1 to a value V1. If V1 is not false, it is returned without

evaluating E2. If V1 is false, the value of E2 is returned. Bud Lojack suggests the following desugaring rule for or: (or E1 E2) ❀ (let ((x E1)) (if x x E2)) Unfortunately, this desugaring has a bug. Give a concrete expression in which Bud’s desugaring fails to have the right meaning. Back

CS251 Jeopardy Spring’05 – p.21/42

slide-22
SLIDE 22

Xforms 5

Give a translation of the following FOFL program into POSTFIX. You may use bget in your translation. (fofl (a b) (f (sq a) (sq b)) (def (sq x) (* x x)) (def (f x y) (/ (+ x y) (- x y)))) Back

CS251 Jeopardy Spring’05 – p.22/42

slide-23
SLIDE 23

Imperative 1

List all of the following languages in which a variable is always bound to an implicit mutable cell.

SCHEME OCAML JAVA HASKELL C

Back

CS251 Jeopardy Spring’05 – p.23/42

slide-24
SLIDE 24

Imperative 2

What programming language property corresponds to the mathematical notion of “substituting equals for equals” (Pureley functional languages have it; imperative languages don’t.) Back

CS251 Jeopardy Spring’05 – p.24/42

slide-25
SLIDE 25

Imperative 3

What is the value of executing f(5), where f is the follow- ing C function? int f (int n) { int ans = 1; while (n > 0) { n = n - 1; ans = n * ans;

} return ans; }

Back

CS251 Jeopardy Spring’05 – p.25/42

slide-26
SLIDE 26

Imperative 4

What is the value of executing g(1,2) in the context of the following C definitions? void h (int x, int* y) { x = x + *y; *y = *y + x;

} int g (int a, int b) { h(a, &b); return a * b; }

Back

CS251 Jeopardy Spring’05 – p.26/42

slide-27
SLIDE 27

Imperative 5

What is the value of the following program in statically- scoped call-by-value HOILIC? Assume operands are evalu- ated from left to right. (Hint: draw environments!)

(bind f (bind a 0 (fun () (seq (<- a (+ a 1)) (bindpar ((b a) (c 0)) (fun () (seq (<- c (+ c b)) c)))))) (bindseq ((p (f)) (q (f))) (list (p) (q) (p) (q))))

Extra: What if (+ c b) were changed to (+ c a)?

Back

CS251 Jeopardy Spring’05 – p.27/42

slide-28
SLIDE 28

Control 1

Edsgar Dijkstra considered this control construct harmful. Back

CS251 Jeopardy Spring’05 – p.28/42

slide-29
SLIDE 29

Control 2

Which one of the following most closely resembles

PASCAL’s goto construct? SCHEME’s error SCHEME’s call-with-current-continuation OCAML’s raise JAVA’s break JAVA’s try/catch

Back

CS251 Jeopardy Spring’05 – p.29/42

slide-30
SLIDE 30

Control 3

What is the value of the following expression in a version of

SCHEME supporting raise and handle?

(handle err (lambda (y) (+ y 200)) (let ((f (lambda (x) (+ (raise err x) 1000)))) (handle err (lambda (z) (+ z 50)) (f 4)))

Extra: what if the handles are replaced by traps?

Back

CS251 Jeopardy Spring’05 – p.30/42

slide-31
SLIDE 31

Control 4

Consider the following procedure in a version of SCHEME supporting label and jump:

(define test (lambda (x) (+ 1 (label a (+ 20 (label b (+ 300 (jump a (label c (if (> x 0) (+ 4000 (jump c x)) (jump b x)))))))))))

What is the value of the expression (+ (test 0) (test 5))? Back

CS251 Jeopardy Spring’05 – p.31/42

slide-32
SLIDE 32

Control 5

What is the value of the following expression in a version of

SCHEME supporting label and jump?

(let ((twice (lambda (f) (lambda (x) (f (f x))))) (inc (lambda (x) (+ x 1)))) (let ((g (label a (lambda (z) (jump a z))))) (((g twice) inc) 0)))

Back

CS251 Jeopardy Spring’05 – p.32/42

slide-33
SLIDE 33

Types 1

Name two "real-world" statically-typed language that do not require explicit types. Back

CS251 Jeopardy Spring’05 – p.33/42

slide-34
SLIDE 34

Types 2

What feature is lacking in Java’s type system that makes it impossible to write a general Scheme or ML style map function in Java? Back

CS251 Jeopardy Spring’05 – p.34/42

slide-35
SLIDE 35

Types 3

What is the name of a transformation that can transform an OCAML function of type int * char -> bool to a function of type int -> char -> bool ? Back

CS251 Jeopardy Spring’05 – p.35/42

slide-36
SLIDE 36

Types 4

Write a declaration of an OCAML function f that has the following type:

(’a -> ’b list) -> (’b -> ’c list) -> (’a -> ’c list)

You may find it helpful to use the following list functions in your definition:

List.map: (’a -> ’b) -> (’a list) -> (’b list) List.flatten (’a list list) -> (’a list)

Back

CS251 Jeopardy Spring’05 – p.36/42

slide-37
SLIDE 37

Types 5

For each of the following OCAML function declarations, either write down the type that would be reconstructed for the function or indicate that no type can be reconstructed:

let test1 (x, f, g) = (x, f(x), g(x)) let test2 (x, f, g) = (x, f(x), g(f(x))) let test3 (x, f, g) = (x, f(x), g(f(x)), f(g(x))) let test4 (x, f, g) = (x, f(x), g(x, f(x))) let test5 (x, f, g) = (x, f(x), g(f(x), f(g(x)))) let test6 (x, f, g) = (x, f(x), g(x, f(g(x))))

Back

CS251 Jeopardy Spring’05 – p.37/42

slide-38
SLIDE 38

Potpourri 1

Who was the inventor of the lambda calculus, a formal system upon which functional programming is based? Back

CS251 Jeopardy Spring’05 – p.38/42

slide-39
SLIDE 39

Potpourri 2

Complete the following Guy Steele poem by filling in the ???: A one slot cons is called a ??? A two-slot cons makes lists as well And I would bet a coin of bronze There isn’t any three-slot cons. Back

CS251 Jeopardy Spring’05 – p.39/42

slide-40
SLIDE 40

Potpourri 3

Is it possible to write an interpreter for an imperative language in a purely functional language? Back

CS251 Jeopardy Spring’05 – p.40/42

slide-41
SLIDE 41

Potpourri 4

List five properties that values must have in order to be considered “first-class”. Back

CS251 Jeopardy Spring’05 – p.41/42

slide-42
SLIDE 42

Potpourri 5

We saw how to automatically translate FOFL programs to

POSTFIX programs. Answer both of the following:

  • 1. Describe a simple approach for translating FOBS

programs to POSTFIX.

  • 2. What feature does postfix lack that makes it difficult to

translate HOFL programs to POSTFIX? Back

CS251 Jeopardy Spring’05 – p.42/42