Question 1 @ 2025-03-11 19:52
Which of the following definitions would be correctly implementing division?
(For simplicity, assume that it should work only for inputs that are integer-divisible.)
-
(define / (lambda (m n) (if (zero? m) 0 (add1 (/ (- m n) n)))))
-
(define/rec / (lambda (m n) (if (zero? m) 0 (add1 (/ (- m n) n)))))
-
(define/rec / (lambda (m n) (if (zero? m) 1 (add1 (/ (- m n) n)))))
-
(define/rec / (lambda (m n) (if (zero? m) 0 (add1 (/ m n)))))
-
(define / (lambda (m n) (if (zero? m) 1 (add1 (/ (- m n) n)))))
Question 2 @ 2025-03-11 19:55
We now know that Racket has for-each
function that is similar to map
except that it doesn’t collect the results into a list. Many languages
have a similar functionality, for example, JS has x.forEach(...)
.
Why is such functionality needed?
-
It’s not really needed.
-
It’s a good signal for readers (eg, when debugging) that you’re calling this for side-effects only.
-
It’s a nice-to-have memory optimization.
-
It’s a necessary save-memory feature, so we don’t hold onto expensive resources.
Question 3 @ 2025-03-11 19:56
What is the expected result of the following plain Racket code?
(let ([b (box 0)])
(lambda (msg)
(match msg
['+1 (set-box! b (+ 1 (unbox b)))]
['*2 (set-box! b (* 2 (unbox b)))]
['get (unbox b)]))))
(define thing-a (make-thing))
(thing-a '+1)
(define thing-b (make-thing))
(thing-a '*2)
(thing-b '+1)
(list (thing-a 'get) (thing-b 'get))
-
'(0 0)
-
'(0 1)
-
'(1 1)
-
'(1 2)
-
'(2 1)
-
'(2 2)
-
'(2 3)
-
'(3 2)
- Runtime error
- Syntax error
Question 4 @ 2025-03-11 19:58
In the context of implementing a programming language with a set!
operation to modify variables, what is an advantage of choosing to
return the value that was assigned as the behavior for set!
expressions?
-
It is more efficient at runtime.
-
It is easier to compile.
-
It enables chaining assignments and combining mutations, and using their results.
-
It makes program development more explicit and therefore more robust.
-
It aligns with Racket’s approach to variable mutation.
-
There’s no advantage — on the contrary: it can lead to race conditions.
-
None of the above.
Question 5 @ 2025-03-11 20:00
Reminder: in Typed Racket, the following numerical subtype relationships hold:
Which of the following function types are a subtype of
Integer -> Integer
?
-
Zero -> Zero
-
Zero -> Integer
-
Zero -> Number
-
Integer -> Zero
-
Integer -> Integer
-
Integer -> Number
-
Number -> Zero
-
Number -> Integer
-
Number -> Number