CS251 Jeopardy Spring 2002 CS251 Jeopardy Spring02 p.1/42 - - PowerPoint PPT Presentation

cs251 jeopardy
SMART_READER_LITE
LIVE PREVIEW

CS251 Jeopardy Spring 2002 CS251 Jeopardy Spring02 p.1/42 - - PowerPoint PPT Presentation

CS251 Jeopardy Spring 2002 CS251 Jeopardy Spring02 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 2002

CS251 Jeopardy Spring’02 – 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’02 – 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’02 – p.3/42

slide-4
SLIDE 4

Data 2

What feature in Scheme, ML, and Java is responsible for reclaiming storage used by values that are no longer accessible from the program? Back

CS251 Jeopardy Spring’02 – p.4/42

slide-5
SLIDE 5

Data 3

ML ’s datatype and Haskell’s data construct are examples

  • f "sum-of-product" data type declarations. What are

traditional names for "sum" and "product " in programming languages? Back

CS251 Jeopardy Spring’02 – p.5/42

slide-6
SLIDE 6

Data 4

What is the value of the following ML program? let val yourMom = [[1,2], [3,4,5,6,7], [8]] in map (foldr (fn (_,x) => 1+x) 0) yourMom end Back

CS251 Jeopardy Spring’02 – p.6/42

slide-7
SLIDE 7

Data 5

What problem does invoking the following C function lead to? int* elts (int c, int n) { int a[n]; int i; for (i = 0; i < n; i++) { a[i] = c*i;

} return a; }

Extra: : How can the problem be fixed?

Back

CS251 Jeopardy Spring’02 – p.7/42

slide-8
SLIDE 8

Naming 1

List all of the free variables of the following HOFL expression: (abs (a) (a b (abs (b) (+ b c)))) Back

CS251 Jeopardy Spring’02 – p.8/42

slide-9
SLIDE 9

Naming 2

List all of the following languages that are block structured: Pascal C Java Scheme ML Back

CS251 Jeopardy Spring’02 – p.9/42

slide-10
SLIDE 10

Naming 3

The following is a legal program in both FOBS and HOFL. In FOBS, it denotes the factorial function, while in HOFL it does not. What programming language feature accounts for the difference in meaning between the two languages? (program (n) (funrec ((fact (fact) (if (= fact 0) 1 (* fact (fact (- fact 1)))))) (fact n))) Back

CS251 Jeopardy Spring’02 – p.10/42

slide-11
SLIDE 11

Naming 4

Give the value of the following expression in both lexically 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’02 – p.11/42

slide-12
SLIDE 12

Naming 5

Give the value of the following Scheme expression under all four parameter passing mechanisms: call-by-value, call-by-name, call-by-need, call-by-reference. Assume procedure arguments are evaluated in left-to-right order. (let ((a 1)) (let ((b a)) (let ((c (begin (set! a (* a 2) a)))) (begin (set! b 10) (+ a (+ c c)))))) Back

CS251 Jeopardy Spring’02 – 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’02 – 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’02 – 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 Enonzero) desugars to (if (= Enum 0) Ezero Enonzero) (2) (define if0 (lambda (Enum Ezero Enonzero) (if (= Enum 0) Ezero Enonzero))) List all of the following parameter-passing mechanisms under which the two definitions are equivalent: call-by-value call-by-name call-by-need Back

CS251 Jeopardy Spring’02 – 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 elts) where elts = 1 : (map (2 *) elts) Back

CS251 Jeopardy Spring’02 – 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 ((inc! (lambda (x) (begin (set! n (+ n x)) n)))) (let ((inc1 (lambda () (inc! 1))) (inc2 (delay (inc! 2)))) (+ (* (inc1) (force inc2)) (* (inc1) (force inc2))))))

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

Back

CS251 Jeopardy Spring’02 – 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’02 – p.18/42

slide-19
SLIDE 19

Xforms 2

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

CS251 Jeopardy Spring’02 – p.19/42

slide-20
SLIDE 20

Xforms 3

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’02 – p.20/42

slide-21
SLIDE 21

Xforms 4

Consider the following transformation in an imperative version of Scheme: ((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-name call-by-need call-by-reference Back

CS251 Jeopardy Spring’02 – p.21/42

slide-22
SLIDE 22

Xforms 5

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 E2is returned. Bud Lojack suggests the following desugaring rule for (or E1 E2): (or E1 E2) desugars to (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’02 – p.22/42

slide-23
SLIDE 23

Imperative 1

List all of the following languages in which a variable is always bound to an explicit mutable cell. Scheme ML Java Haskell C Back

CS251 Jeopardy Spring’02 – p.23/42

slide-24
SLIDE 24

Imperative 2

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

CS251 Jeopardy Spring’02 – p.24/42

slide-25
SLIDE 25

Imperative 3

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

} return ans; }

Back

CS251 Jeopardy Spring’02 – 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’02 – p.26/42

slide-27
SLIDE 27

Imperative 5

What is the value of the following Scheme program? Assume operands are evaluated from left to right. (Hint: draw environments!)

(let ((f (let ((a 0)) (lambda () (begin (set! a (+ a 1)) (let ((b 0)) (lambda () (begin (set! b (+ a b)) b)))))))) (let ((p (f)) (+ (p) (let ((q (f)) (+ (q) (+ (p) (q))))))

Back

CS251 Jeopardy Spring’02 – p.27/42

slide-28
SLIDE 28

Control 1

Name the property that allows Scheme to perform iterations in constant space without explicit looping constructs. Back

CS251 Jeopardy Spring’02 – p.28/42

slide-29
SLIDE 29

Control 2

Which one of the following most closely models Pascal’s goto construct? Scheme’s error construct Scheme’s call-with-current-continuation construct ML ’s raise construct Java’s try/catch construct Java’s break construct Back

CS251 Jeopardy Spring’02 – p.29/42

slide-30
SLIDE 30

Control 3

What is the value of the following expression in a version

  • f 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’02 – 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))? Assume operands are evaluated left-to-right.

CS251 Jeopardy Spring’02 – p.31/42

slide-32
SLIDE 32

Control 5

What is the value of the following expression in a version

  • f Scheme supporting label and jump?

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

Back

CS251 Jeopardy Spring’02 – p.32/42

slide-33
SLIDE 33

Types 1

Name a "real-world" statically-typed language that does not require explicit types. Back

CS251 Jeopardy Spring’02 – 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’02 – p.34/42

slide-35
SLIDE 35

Types 3

What type would the ML type reconstructer infer for the following function definition? fun some pred []= NONE | some pred (x::xs) = if (pred x) then SOME(x) else some pred xs Back

CS251 Jeopardy Spring’02 – p.35/42

slide-36
SLIDE 36

Types 4

Write an explicitly typed HOFLEMT expression that has the following type. The function must actually use each of its arguments. (-> (int (-> (int) bool)) bool) Back

CS251 Jeopardy Spring’02 – p.36/42

slide-37
SLIDE 37

Types 5

Translate the following (implicitly typed) HOFLIPT expression into an explicitly typed HOFLEPT expression with the most general possible type. (abs (f x) (f x x))

Extra: What is the type of your HOFLEPT expression?

Back

CS251 Jeopardy Spring’02 – p.37/42

slide-38
SLIDE 38

Potpourri 1

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’02 – p.38/42

slide-39
SLIDE 39

Potpourri 2

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

CS251 Jeopardy Spring’02 – 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’02 – p.40/42

slide-41
SLIDE 41

Potpourri 4

Fill in the ??? in the following Norman Adams quote: “Objects are a poor man’s ???”. Back

CS251 Jeopardy Spring’02 – p.41/42

slide-42
SLIDE 42

Potpourri 5

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

CS251 Jeopardy Spring’02 – p.42/42