Lab 12 - Macros

Instructions: https://inst.eecs.berkeley.edu/~cs61a/su19/lab/lab12/#required-problems

Solution: https://github.com/tomthestrom/cs61a/blob/master/lab/lab12/lab12.scm

The first 2 questions are non-coding - What Would Scheme Do?

Q3: Repeatedly Cube

Implement the following function, which cubes the given value x some number n times, based on the given skeleton.

(define (repeatedly-cube n x)
    (if (zero? n)
        x
        (let
            (_________________________)
            (* y y y))))

Q3: Solution

(define (repeatedly-cube n x)
    (if (zero? n)
        x
        (let
            ((y (repeatedly-cube (- n 1) x)))
            (* y y y)
            )
        ))

Q4: Scheme def

Implement def, which simulates a python def statement, allowing you to write code like (def f(x y) (+ x y)).

The above expression should create a function with parameters x and y, and body (+ x y), then bind it to the name f in the current frame.

(define-macro (def func bindings body)
    'YOUR-CODE-HERE)

Q4: Solution

(define-macro (def func bindings body)
    `(define ,func (lambda ,bindings ,body))
    )

Last updated