Your First Program¶
Running a file¶
Create a file called hello.scm:
Run it:
Output:
The REPL¶
Launch the REPL with no arguments:
The REPL provides:
- Line editing -- arrow keys, Ctrl-A (start of line), Ctrl-E (end of line), backspace, delete
- Command history -- up/down arrows, persisted across sessions in
.kaappi_history - Tab completion -- completes all built-in and user-defined symbols
- Multi-line input -- open parentheses are tracked; the prompt changes to
...until all parens are balanced
kaappi> (define (square x)
... (* x x))
kaappi> (square 7)
49
kaappi> (map square '(1 2 3 4 5))
(1 4 9 16 25)
Type (exit) or press Ctrl-D to quit.
No install yet?
You can try all these examples in the browser playground — same interpreter, no installation required.
A Multi-File Project¶
Real programs split code into libraries. Here's a small project that computes statistics on a list of numbers.
Create the project structure:
Write the library in mylib/stats.sld:
(define-library (mylib stats)
(export mean median)
(import (scheme base)
(srfi 1) ;; for fold
(srfi 132)) ;; for list-sort
(begin
(define (mean lst)
(/ (fold + 0 lst) (length lst)))
(define (median lst)
(let* ((sorted (list-sort < lst))
(n (length sorted))
(mid (quotient n 2)))
(if (odd? n)
(list-ref sorted mid)
(/ (+ (list-ref sorted (- mid 1))
(list-ref sorted mid))
2))))))
Write the main program in main.scm:
(import (scheme base)
(scheme write)
(mylib stats))
(define data '(7 2 9 4 1 8 3 6 5))
(display "Data: ") (display data) (newline)
(display "Mean: ") (display (inexact (mean data))) (newline)
(display "Median: ") (display (median data)) (newline)
Run it from inside stats-demo/:
Output:
The key points:
- Library name
(mylib stats)maps to file pathmylib/stats.sld exportcontrols what the library exposesimportin both the library and main program pulls in dependencies- Kaappi finds
mylib/stats.sldautomatically from the current directory
For more on libraries, import modifiers, and search paths, see Working with Libraries.
Next: Scheme Tutorial