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 replYou'll see a prompt:
>
This is where you type expressions. The REPL:
- Reads your input
- Evaluates it
- Prints the result
- Loops back for more
Your First Expressions
Try some arithmetic:
> (+ 1 2)
3
> (* 6 7)
42
> (- 100 58)
42
> (/ 10 3)
3.33333Note 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))
21The 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)
#\HCharacters 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.28318Once defined, you can use the name anywhere:
> (define radius 5)
> (* pi (* radius radius))
78.53975Defining Procedures
Procedures (functions) are defined with define too:
> (define (square x) (* x x))
> (square 5)
25
> (square 12)
144The 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)
49Both 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))
5Lists 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
| Action | How |
|---|---|
| Evaluate expression | Type it and press Enter |
| Define value | (define name value) |
| Define procedure | (define (name args) body) |
| Exit | Ctrl+D |
What's Next
Now that you're comfortable with the REPL, let's dive deeper into Sigil's syntax and basic concepts.