Question 1 @ 2024-11-12 18:44
What’s the purpose of a compiler — what does it do?
(Choose the best answer.)
- Translate code in some language to machine code.
- Translate code in a high level language to a lower level language.
- Translate code in some language to some different language so it runs faster.
- Translate code to some other code, possibly in a different language.
- If we want to be precise, is any kind of code processing.
Question 2 @ 2024-11-12 18:45
What’s wrong with the following compiler?
(define (compile expr)
(cases expr
[(Num n) (lambda ([env : ENV]) (RktV n))]
[(Id name) (lambda ([env : ENV]) (lookup name env))]
...
[(Call fun-expr arg-exprs)
(define cfun (compile fun-expr))
(define compiled-args (map compile arg-exprs))
(lambda ([env : ENV])
(define fval ...)
(define arg-vals ...)
(cases fval
[(FunV names body fun-env)
(define compiled-body (compile body))
(compiled-body (extend names arg-vals fun-env))]
...))]
...))
(cases expr
[(Num n) (lambda ([env : ENV]) (RktV n))]
[(Id name) (lambda ([env : ENV]) (lookup name env))]
...
[(Call fun-expr arg-exprs)
(define cfun (compile fun-expr))
(define compiled-args (map compile arg-exprs))
(lambda ([env : ENV])
(define fval ...)
(define arg-vals ...)
(cases fval
[(FunV names body fun-env)
(define compiled-body (compile body))
(compiled-body (extend names arg-vals fun-env))]
...))]
...))
- There is no problem.
-
The use of
compile
is wrong, and will lead to a type error. -
Using
compile
at runtime is wrong, we should useeval
instead. -
The
compile
function should never call itself. -
The
compile
function should never emit code that calls itself.
Question 3 @ 2024-11-12 18:48
What will the following evaluate to in the lazy PL language?
(define (my-if cond then else) (if cond then else))
(my-if (list (/ 1 0)) 0 1)
(my-if (list (/ 1 0)) 0 1)
- Division by zero error
-
Syntax error due to using
cond
as a keyword - Infinite loop
Question 4 @ 2024-11-12 18:49
We know that we can define if
as a plain function in lazy Racket:
(define (my-if cond then else) (if cond then else))
And in fact, if
(the builtin value) is itself a function. Can the same
be said on cond
?
Yes, No
Question 5 @ 2024-11-12 18:51
Assume this code in the lazy PL language:
(define foo (cons 1 (cons 2 (cons 3 bar))))
(define bar (cons 'boom (map add1 foo)))
(define bar (cons 'boom (map add1 foo)))
How does the value that foo
is bound to look like?
- It’s an infinite loop, nothing to see.
- Error due to an infinite loop
-
Error due to trying to use
add1
with a symbol -
(1 2 boom 3 4 boom 5 6 boom ...)
-
(1 2 boom 2 3 boom 3 4 boom ...)
-
(1 2 #<error> 3 4 #<error> 5 6 #<error> ...)
-
(1 2 #<error> 2 3 #<error> 3 4 #<error> ...)
-
(1 2 boom 3 4 #<error> 5 6 #<error> ...)
-
(1 2 boom 2 3 #<error> 3 4 #<error> ...)
Question 6 @ 2024-11-12 18:54
Of the following Racket (or #lang pl
) features, which ones are side-effects?
if
set-box!
unbox
random
define
error