Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 38 ยท Getting Started

The REPL and Developer Tools

Use the Racket REPL and DrRacket effectively: interactive evaluation, the stepper, error messages, printing values, comments, and the raco command-line tools.

Lisp programmers work interactively. Instead of write-compile-run-wait, you keep a REPL (read-eval-print loop) open, try small expressions, and build your program piece by piece. This lesson shows how to get the most from that workflow in Racket, how to read its error messages, and which tools help when something goes wrong.

The REPL transcripts here are real Racket sessions; the Run buttons use the JavaScript stand-in, which prints results in the same style for these small examples.

The REPL loop

Start it by typing racket in a terminal (or use DrRacket's bottom pane). You see a > prompt. The REPL reads what you type, evaluates it, prints the result, and loops:

RACKET
> (+ 1 2)
3
> (define x 10)
> (* x x)
100
> (list 1 2 3)
'(1 2 3)
> "hello"
"hello"
> 'apple
'apple

Three details worth noticing:

  • (define x 10) prints nothing: a definition returns void, and void results are not printed.
  • Lists and symbols print with a leading ' because that is how you would write them in code. '(1 2 3) is a quoted list.
  • Strings print in double quotes. (displayln "hello") prints hello without quotes, because it displays rather than writes.
REPL-style evaluation

Racket has three ways to output a value, and the difference matters when you work with strings and symbols:

Function Output for "hi" Output for '(a "b") Used for
display hi (a b) Human-readable text
write "hi" (a "b") Machine-readable, can be read back
print "hi" '(a "b") What the REPL shows, valid code

Each has a ln flavour (displayln, println) that adds a newline. You will use displayln the most.

display versus write

printf uses ~a (display style), ~s (write style) and ~n (newline). format does the same but returns a string.

Comments

RACKET
; line comment: runs to the end of the line
(+ 1 2) ; also fine at the end of a line

#| block comment
   spans several lines, #| and nests |# |#

#;(this whole expression is ignored)   ; datum comment

The #; datum comment is handy: it comments out exactly the next expression, however long, without counting parentheses.

Reading error messages

Racket's errors are precise and give you the kind of problem plus the offending value. Here are the four you will see most:

RACKET
> (car 5)
car: contract violation
  expected: pair?
  given: 5

> (foo 1)
foo: undefined;
 cannot reference an identifier before its definition

> ((lambda (x) x))
arity mismatch;
 the expected number of arguments does not match the given number
  expected: 1
  given: 0

> (5 3)
application: not a procedure;
 expected a procedure that can be applied to arguments
  given: 5
  • A contract violation means you passed the wrong kind of value.
  • undefined means a misspelled name, or you used it before its define.
  • arity mismatch means the wrong number of arguments.
  • not a procedure usually means extra parentheses: (5 3) tries to call 5. In Lisp, parentheses mean call, so ((f x)) is not the same as (f x).
Provoke an error

The program stops at the first uncaught error. Edit the program to (car (list 5)) and run it again to see the fixed version.

DrRacket's tools

  • Check Syntax: click it and DrRacket draws arrows from each name to where it was defined, highlights unused variables, and lets you right-click to rename a variable safely across the file.
  • Stepper: in the teaching languages and #lang racket, the Stepper button shows evaluation one reduction at a time, rewriting (+ 1 (* 2 3)) into (+ 1 6) and then 7. It is the best way to learn how evaluation works.
  • Debugger: set breakpoints and inspect variables.
  • Macro Stepper: shows how macros expand (used in the macros chapter).
  • Indentation: press Tab or Ctrl+I to reindent. Never fix indentation by hand: correct indentation reveals bracket mistakes.
  • Typing a \lambda followed by Ctrl+\ inserts the character ฮป, which Racket accepts in place of lambda.

Command-line helpers

Terminal
raco docs map          # open the documentation at the entry for map
raco test file.rkt     # run tests in a file
raco make file.rkt     # compile to bytecode
raco pkg install rackunit-lib
raco exe file.rkt      # make a standalone executable

For quick experiments from a shell, racket -e '(displayln (+ 1 2))' runs one expression.

A workflow that works

  1. Write one small function in the definitions pane.
  2. Press Run and try it in the REPL with an easy case, then an edge case (empty list, zero, negative).
  3. Only then write the next function.

That loop of define, run, poke, fix is faster than any compile-and-test cycle, and it is how most Lisp code is written.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the REPL print after (define x 10)?

    define returns void, and the REPL does not print void values.
  2. 2. How do display and write differ for the string "hi"?

    display is for people, write is for output that can be read back as data.
  3. 3. What error does (5 3) cause?

    Parentheses mean call, so Racket tries to call the number 5 as a function with the argument 3.
  4. 4. What does #; do?

    #; is a datum comment: the next whole expression is ignored.

Summary

  • The REPL reads, evaluates, prints, loops; work in small experimental steps.
  • Definitions print nothing; lists and symbols print quoted like code; displayln prints plain text.
  • display is for people, write for round-trippable data, print for code-style values.
  • Racket errors name the kind of problem: contract, undefined, arity, not a procedure.
  • DrRacket's Check Syntax, Stepper and debugger plus raco cover most needs.

Next: Racket, Scheme Dialects and the Ecosystem