Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 38 ยท Projects and Practice

Practice: Macros and Course Review

Practice macros, continuations, contracts and modules with exercises and solutions, a runnable macro-expander challenge, and a final review of the Racket and Scheme course.

You have now met tail calls, continuations, macros, languages, modules, contracts, tests and applications. This last lesson gives you exercises that combine them, a runnable challenge for the macro expander, and a map of the whole course so you know where to go next.

As everywhere in this course, Racket cannot run on our site: Racket code is read-only, and the runnable blocks are JavaScript stand-ins. Solutions are shown after each exercise; try your own first.

A map of the chapters

Control and evaluation

Tail calls Loops as calls
call/cc Escape and resume
Generators Pause and continue

Control flow is a value you can hold.

1 / 3

Exercise 1: write a macro

Write while, a loop macro: (while test body ...) repeats the body as long as test is true.

RACKET
(define-syntax-rule (while test body ...)
  (let loop ()
    (when test
      body ...
      (loop))))

(define i 0)
(while (< i 3)
  (displayln i)
  (set! i (+ i 1)))
;; prints 0, 1, 2

Why let loop ()? It is a named let (tail calls): the recursive call is in tail position inside when, so the loop runs in constant space. Because of hygiene (hygiene lesson), the macro's loop cannot clash with a user variable named loop. Try it: can you write a version that supports break? (Hint: wrap the loop in let/ec and bind break with a syntax parameter.)

Exercise 2: a macro expander challenge

Using the expander idea from syntax-rules macros, this stand-in expands a my-while-style form with ellipsis patterns. Your task in the editor: add a my-unless rule and a rule for (inc! x) which expands to (set! x (+ x 1)), then check the printed expansions.

JS Stand-in: extend the expander

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first result shows inc! expanded inside the output of while: expansion is repeated until no rule matches, as in Racket.

Exercise 3: contracts and modules

Design a module stack.rkt for an immutable stack. Provide empty-stack, push, pop, top and stack-size with contracts. A solution:

RACKET
#lang racket
(struct stack (items))               ; constructor stays private

(provide (contract-out
          [empty-stack stack?]
          [push (-> stack? any/c stack?)]
          [pop (-> (and/c stack? (not/c stack-empty?)) stack?)]
          [top (-> (and/c stack? (not/c stack-empty?)) any/c)]
          [stack-size (-> stack? natural?)]
          [stack-empty? (-> stack? boolean?)]))

(define empty-stack (stack '()))
(define (push s x) (stack (cons x (stack-items s))))
(define (pop s) (stack (cdr (stack-items s))))
(define (top s) (car (stack-items s)))
(define (stack-size s) (length (stack-items s)))
(define (stack-empty? s) (null? (stack-items s)))

(module+ test
  (require rackunit)
  (check-equal? (top (push empty-stack 5)) 5)
  (check-exn exn:fail:contract? (lambda () (pop empty-stack))))

Note how pop on an empty stack is blamed on the caller by the contract (contracts lesson), not discovered inside cdr. The module+ test follows rackunit practice.

Exercise 4: continuations

Write find-first with an escape, as in call/cc and Escapes, and use let/ec instead of full call/cc:

RACKET
(define (find-first pred lst)
  (let/ec return
    (for ([x lst])
      (when (pred x) (return x)))
    #f))

Then extend it to (find-all-before pred lst), collecting elements until the first one that satisfies pred.

Review quiz

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which tool lets a function exit several nested loops at once?

    Calling the captured continuation returns immediately from the whole expression.
  2. 2. Why use a macro for while rather than a function?

    As a function, the test and body would be evaluated once before the call. A macro rewrites the code so they run on every iteration.
  3. 3. Which statement about contracts is correct?

    Contracts wrap exports; tests are still needed for behavior.
  4. 4. What gives Scheme and Racket loops without a loop keyword?

    Calls in tail position reuse the stack frame, so recursion expresses loops safely.

Where to go next

  • Build something: a small language with #lang, a web app with dispatch-rules, or a game with big-bang.
  • Read a classic: Structure and Interpretation of Computer Programs (Scheme) and How to Design Programs (Racket) are both free online.
  • Explore advanced Racket: typed Racket, syntax-parse macros, racket/contract with parametric contracts, Rosette for solver-aided programming, and raco pkg to publish a package.
  • Return to the track page to review any chapter.

Summary

  • Macros control evaluation and binding; hygiene keeps them safe; named let and tail calls give constant-space loops.
  • Escapes (let/ec, call/cc) are the practical use of continuations.
  • Modules provide structure; contracts guard their boundaries; rackunit checks behavior.
  • Applications (text, web, concurrency, graphics) build on the same small core ideas.

Next: Back to the Racket and Scheme track