


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) (5pts) Give two fundamental differences between the ML and the Scheme languages. (1) Scheme is interpreted, ML is both compiled and interpreted (2) Scheme supports ad-hoc polymorphism, ML Give two fundamental differences between the ML and the C languages. (1) ML is both compiled and interpreted; C is purely compiled (2) ML variables cannot be modified unless it is If we were to allow function definitions to be nested and function pointers to be returned as results in the C language, what additional support do we need to add to C? managing activation records in the heap and d (b) (5pts) A type is a collection of computable values that share some structural properties. There are two kinds of type declarations to introduced new type names, transparent declarations and opaque declarations. There are two ways to determine whether two types are equivalent, name equivalence and structural equivalence. (c) (5pts) A type error occurs when a value is misinterpreted at runtime. A language is type safe if it does not allow type errors at runtime. An example language that is type safe is ML/Scheme/Java; an example language that is not type safe is C/C++/Pascal/Fortran. Give a key difference be- tween compile-time and runtime type checking (aside from that one is done at compile time and the other done at runtime): Every variable can have only a single type in compile-time type checking; Variables can (d) (5pts) A function is polymorphic if it can operate on different types of input parameters. There are three kinds of polymorphism: (1) ad-hoc polymorphism, (2) parametric polymorphism and (3) subtype polymorphism. A compiler is said to perform type inference if it automatically determine the type of each (e) (5pts) A block is a code fragment that introduces new variables. The runtime memory of a program includes three components, the code space (which contains the instructions to be eval- uated), the runtime stack, and the heap. There are three ways to implement parameter passing for function calls: pass-by-name, pass-by-reference, and pass-by-value. (f) (5pts) A tail call is a function call that there is no more computation in the caller after the call returns. In particular, tail recursion is equivalent to loops because The callee can reuse the activation record of the caller b An exception is when something unexpected happens, we want the program to jump out of the current block(pop until a caller block can handle the exception. The concept of continuation means. Both continuation and exceptions can be used to support dynamic jumps in programs.
(define Append (lambda (x y) (if (null? x) y (cons (car x) (Append (cdr x) y)))))
x: ’a list; y: ’a list; return type: ’a list fun Append([], y) = y | Append(x1::x2, y) = x1 :: Append(x2, y);
(define Add (lambda (x y) (cond ((number? y) (+ x y)) ((null? y) ’()) (else (cons (Add x (car y)) (Add x (cdr y)))))))
Use the following ML datatype in your translation (Hint: x and y do not have the same type)
datatype Value = Number of int | Tree of Value*Value | NULL;
fun Add(x, Number(y)) = Number(x + y) | Add(x, NULL) = NULL | Add(x, Tree(y1,y2)) = Tree(Add(x,y1), Add(x,y2));
1: let 2: fun mk_x(x) = 3: let fun add1(y) = x + y 4: in 5: let val x = 7 in add1(5) end 6: end 7: fun apply(f,x) = f(x) 8: in 9: apply(mk_x,10)- 10:end;
Global: Control Link -> NULL Access Link -> NULL mk_x : <Global, impl of mk_x> apply: <Global, impl of apply> apply(mk_x,10):?
apply(mk_x,10): Control Link -> Global Access Link -> Global Return Addr -> line 9 Return Result Addr -> Global.apply(mk_x,10) f -> <Global, impl of mk_x> x : 10 mk_x(10) :
mk_x(10): Control Link -> apply(mk_x,10) Access Link -> Global Return Addr -> line 7 Return Result Addr -> apply(mk_x,10).mk_x(10) x : 10 add1: <mk_x(10), impl of add1>
Inline1: Control Link -> mk_x(10)
main(3,[]) called; line 7: insert(3,[]) called line 6: find(3,[]) called line 3: exception Missing(3) raised line 6: exception Missing(3) handled line 6: [3] insert(3,[]) returns with result [3] line 7: main(3,[]) returns with result [3]