

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


(a) In programming languages, a block is a region of code that. Blocks can be each other but not each other. An example of a block in Scheme is ; An example of a block in ML is. (b) When programs are loaded to memory to be evaluated, its memory space can be separated into three components, the , which contains ; the , which contains ; and the , which contains. (c) The scoping rule of a programming language defines where to look for the scope of (i.e., the block that declares) non-local variables at runtime. If static scoping is used, we find the scope of a non-local variable by looking in ; in contrast, if dynamic scoping is used, we find the scope of a non-local varable by looking in. (d) The value of a function is called a closure, which contains two fields, the , which points to ; and the , which points to. . (e) There are three ways to pass parameters when invoking a function: pass-by-name, where param- eter names within the function body are substituted by the corresponding arguments. pass-by- , where ; and pass by , where. (f) A function invocation is called a tail call if. In particular, loops are equivalent to. (g) The continuation of an expression is. Continuation passing is a programming technique that. (h) An exception is when something unusual happens, e.g., divide-by-zero, we want the program to jump to until. To support exception handling, a language must provide mechanisms to and to. An exception is considered a dynamic jump because.
(a) (define Add1 (lambda (x) (cond ((null? x) ’()) (else (cons (+ (car x) 1) (Add1 (cdr x))))))) The following examples illustrates how the above function works
(Add1 ’(3 5 7)) (list 4 6 8) (Add1 ’(5 7 8)) (list 6 8 9) (b) (define Mult (lambda (x) (cond ((number? x) x) ((symbol? x) 1) ((null? x) 1)
1
(else (let ((n1 (Mult (car x))) (n2 (Mult (cdr x)))) (* n1 n2)))))) Use the following ML datatype definition in your translation. datatype Value = Number of int | Symbol of string | List of Value list;
1 fun foo3 (x) = 2 let val z = 2. 3 in let fun foo1(y) = 4 x * z + y; 5 fun foo2(f, x) = 6 f(x) 7 in foo2(foo1,x) 8 end 9 end; 10 foo(5); 11
(a) fun delete(x,nil) = nil | delete(x,y::z) = if (x = y) then z else y::(delete(x,z)); (b) datatype Value = Number of int | List of Value list; fun count(x,List[]) = 0 | count (x, Number(y)) = if (x = y) then 1 else 0 | count (x, List(y::z)) = count(x,y) + count(x,List(z));
fun gcd(x,y) = if x = y then x else if (x > y) then gcd(y, x-y) else gcd(x, y-x);
1 exception ExcptString of string; 2 exception ExcptInt of int; 3 4 fun divide(x,y) = if (y = 0) 5 then raise ExcptString("overflow") 6 else if x = 0 then raise ExcptInt(x) else x div y; 7 fun pred(x,y) = divide(y-1,x-2)) 8 handle ExcptString(z) => divide(y-1,x); 9 fun compose(x,y) = (pred(x,y) handle ExcptString(k)=>10) 10 handle ExcptInt(z) => 100;
What is the result of evaluating the expressions compose(2, 1)? List the sequence of events occured during the evaluation. In particular, list which function is called with what arguments, which funtion call returns with what value, what exception gets raised in which function call, and what exception is handled in which function call. List the line number of each event.