Question 1 @ 2024-04-02 18:53
How do macros in an X programming language simplify the life of a language designer who wants to use X to implement a new language Y?
- The two are unrelated: a language designer needs to implement their language anyway.
- The opposite is true: they make the life of a language designer more difficult.
- The give the designer access to the compiler of the host language.
- They allow incremental extensions of the X language to support the constructs that are needed for Y.
- They act as a better mechanism for pre-processing Y code into X.
Question 2 @ 2024-04-02 18:55
Say that you want to implement a swap!
that can swap the values of two
boxes. It would look like this:
> (define b2 (box 2))
> (swap! b1 b2)
> (list (unbox b1) (unbox b2))
'(2 1)
What is the right tool for defining swap!
?
- A function
- A function in a lazy language
- A lisp macro
- A hygienic (= Scheme/Racket) macro
- Impossible to tell
Question 3 @ 2024-04-02 18:57
Say that you want to implement a swap!
that can swap the values of two
variables. It would look like this:
> (define x2 2)
> (swap! x1 x2)
> (list x1 x2)
'(2 1)
What is the right tool for defining swap!
now?
- A function
- A function in a lazy language
- A lisp macro
- A hygienic (= Scheme/Racket) macro
- Impossible to tell
Question 4 @ 2024-04-02 18:59
The last two questions inspired your friend to implement a
with-swapped
thing that evaluates some code with two given boxes
having their values temporarily swapped. Again, an example to
demonstrate, and the implementation:
> (define b2 (box 2))
> (with-swapped b1 b2
(list (unbox b1) (unbox b2)))
'(2 1)
> (list (unbox b1) (unbox b2))
'(1 2)
(let ([v1 (unbox B1)]
[v2 (unbox B2)])
(set-box! B1 v2)
(set-box! B2 v1)
(let ([result body])
(set-box! B1 v2)
(set-box! B2 v1)
result)))
What are the mistakes (or “poor choices”) in this piece of code?
(This question will be worth more than a normal one.)
- It should use a hygienic macro
-
It uses
B1
andB2
multiple times which is bad -
It’s better to use
body ...
to allow multiple expressions (withbegin
on the RHS) -
It’s better to use
body ...
to allow multiple expressions (withvoid
on the RHS) - It could be implemented as a plain function (simpler = better)
- It could be implemented as a function (simpler = better) with a thunk instead