sigildocs

Chapter 2: The REPL

The REPL (Read-Eval-Print Loop) is your interactive playground for experimenting with Sigil. It's where you'll spend most of your development time.

Starting the REPL

sigil repl

You'll see a prompt:

>

This is where you type expressions. The REPL:

  1. Reads your input
  2. Evaluates it
  3. Prints the result
  4. Loops back for more

Your First Expressions

Try some arithmetic:

> (+ 1 2)
3
> (* 6 7)
42
> (- 100 58)
42
> (/ 10 3)
3.33333

Note the prefix notation: the operator comes first, then the operands. This is consistent for everything in Scheme.

Nested Expressions

Expressions can nest:

> (+ 1 (* 2 3))
7
> (* (+ 1 2) (+ 3 4))
21

The inner expressions evaluate first. (+ 1 (* 2 3)) becomes (+ 1 6) which becomes 7.

Trying Things Out

The REPL is for experimentation. Don't be afraid to try things:

> (string-append "Hello, " "World!")
"Hello, World!"
> (string-length "Sigil")
5
> (string-ref "Hello" 0)
#\H

Characters are written with #\ prefix: #\H is the character H.

Defining Values

Use define to create named values:

> (define answer 42)
> answer
42
> (define pi 3.14159)
> (* pi 2)
6.28318

Once defined, you can use the name anywhere:

> (define radius 5)
> (* pi (* radius radius))
78.53975

Defining Procedures

Procedures (functions) are defined with define too:

> (define (square x) (* x x))
> (square 5)
25
> (square 12)
144

The syntax (define (name args...) body) creates a procedure and binds it to name.

You can also write it explicitly with lambda:

> (define square2 (lambda (x) (* x x)))
> (square2 7)
49

Both forms are equivalent.

Multi-line Input

For longer definitions, the REPL handles multiple lines. It waits for matching parentheses:

> (define (greet name)
    (display "Hello, ")
    (display name)
    (display "!")
    (newline))
> (greet "Alice")
Hello, Alice!

Useful Built-in Procedures

Here are some procedures to try:

> (list 1 2 3)
(1 2 3)
> (car (list 1 2 3))
1
> (cdr (list 1 2 3))
(2 3)
> (cons 0 (list 1 2 3))
(0 1 2 3)
> (length (list 1 2 3 4 5))
5

Lists are fundamental in Scheme. We'll explore them in detail later.

Getting Help

To see what a value is:

> (display 42)
42
> (write "hello")
"hello"

display outputs values in human-readable form. write outputs them in a form that can be read back.

Exiting the REPL

Press Ctrl+D or Ctrl+C to exit.

Quick Reference

ActionHow
Evaluate expressionType it and press Enter
Define value(define name value)
Define procedure(define (name args) body)
ExitCtrl+D

What's Next

Now that you're comfortable with the REPL, let's dive deeper into Sigil's syntax and basic concepts.

Next: Basics →