snail-scheme in ten minutes
The whole language at a glance, as one program: values, control, data, typeclasses, mutation, capabilities, suspension and macros, each in a few lines. Read it top to bottom; the chapters on types, capabilities, suspension and values say what each form means, and the tutorials after this one are small programs that use them.
examples/tour.sld:
;; `snail-scheme` in ten minutes. Every line of this file runs:
;;
;; ./snail-scheme run --target node examples/tour.sld
;;
;; prints what `examples/tour.out` holds, and the test suite checks it.
(import
(snail core base)
(snail core io))
;; ---- Values and definitions ----
;; A top-level `define` names a value, and `:` declares its type. The
;; scalars are I32, I64, F32, F64, Bool, Char, String and Unit; an integer
;; literal is an I64 unless something says otherwise.
(define (answer) (→ I64) 42)
;; A function's type is an arrow from its arguments to its result.
(define (square (n I64)) (→ I64)
(* n n))
;; ---- Control ----
;; `if` always has both branches. `cond`, `case`, `when` and `unless` are
;; Scheme's.
(define (sign (n I64)) (→ String)
(cond ((< n 0) "negative")
((= n 0) "zero")
(else "positive")))
;; A named `let` is a loop. A call in tail position never grows the stack.
(define (sum-to (n I64)) (→ I64)
(let loop ((i 1)
(total 0))
(if (> i n) total (loop (+ i 1) (+ total i)))))
;; ---- Lists and functions ----
;; A list holds values of one type and never changes. Functions are values,
;; and `lambda` makes one.
(define (numbers) (→ (List I64)) (list 1 2 3 4 5))
;; ---- Data ----
;; A struct has named fields; an enum has cases, each with its own fields.
;; A type's name builds it, and `(● point x)` reads a stored field.
(record Point
((x I64)
(y I64)))
(enum Shape
(Circle
((center Point)
(radius I64)))
(Rectangle
((corner Point)
(width I64)
(height I64))))
;; `match` takes data apart. A match that misses a case does not compile.
(define (area (shape Shape)) (→ I64)
(match shape
((Circle _ radius) (* 3 (square radius)))
((Rectangle _ width height) (* width height))))
;; There is no null and no exception: absence is an Option, and failure a
;; Result, both ordinary enums.
(define (divide (n I64) (d I64)) (→ (Result I64 String))
(if (= d 0) (Err "division by zero") (Ok (quotient n d))))
;; ---- Generics and typeclasses ----
;; A typeclass names operations, and an instance gives them for one type.
(typeclass (Describe T)
(declare (describe (value T)) (→ String)))
(instance (Describe Point)
(define (describe (point Point)) (→ String)
(string-append "(" (i64->string (● point x)) ", " (i64->string (● point y)) ")")))
(instance (Describe Bool)
(define (describe (b Bool)) (→ String)
(if b "yes" "no")))
;; `All` makes a definition generic, and `requires` asks for instances.
;; Each use is compiled for its own types, so generic code costs what a
;; hand-written copy would.
(define (describe-all (items (List T))) (→ String) (∀ (T)) (⊨ (Describe T))
(string-join (map describe items) " "))
;; ---- Mutation ----
;; Records are heap objects with mutable fields. Aliases see the same
;; writes: whoever holds a record may change it.
(record Counter
((count I64)))
(define (bump! (counter Counter)) (→ Unit)
(set! (● counter count) (+ (● counter count) 1)))
;; ---- Capabilities ----
;; What a function may do to the world is in what it is given. `greet`
;; asks whoever it is handed, and its caller decides who that is: a
;; capability is a value, here a function.
(define (greet (ask (Fn → String))) (→ String)
(string-append "hello, " (ask)))
;; ---- Suspension ----
;; `suspend` stops the body of the `with-prompt` around it and hands its
;; value to that call's handler, with `resume`, the rest of the body, to
;; call at most once (a second call panics): here the handler collects
;; each value emitted and carries on.
(define emitting (→ (Prompt I64 Unit)) (make-prompt))
(define (count-down (n I64)) (→ Unit)
(when (> n 0)
(suspend emitting n)
(count-down (- n 1))))
;; ---- Macros ----
;; Macros are Scheme's `syntax-rules`, and hygienic: `swap!`'s own
;; `temporary` cannot capture the caller's.
(record Label
((text String)))
(define-syntax swap!
(syntax-rules ()
((_ a b) (let ((temporary (● a text)))
(set! (● a text) (● b text))
(set! (● b text) temporary)))))
(define (main (out OutputPort)) (→ Unit)
(write-line out (string-append "seven squared is " (i64->string (square 7))))
;; Nothing converts implicitly: an I64 becomes an F64 only when asked.
(write-line out (f64->string (/ (i64->f64 (answer)) 8.0)))
(write-line out (sign -3))
(display out (sum-to 100))
(newline out)
(write out (map square (filter (lambda (n) (= (remainder n 2) 1)) (numbers))))
(newline out)
(display out (fold-left + 0 (numbers)))
(newline out)
(write out
(map area
(list (Circle (new Point (x 0) (y 0)) 2) (Rectangle (new Point (x 1) (y 1)) 3 4))))
(newline out)
(for-each (lambda (d)
(match (divide 10 d)
((Ok quotient) (write-line out (i64->string quotient)))
((Err reason) (write-line out reason))))
(list 2 0))
(write-line out (describe-all (list (new Point (x 1) (y 2)) (new Point (x 3) (y 4)))))
(write-line out (describe-all (list #t #f)))
(let ((clicks (new Counter (count 0))))
(bump! clicks)
(bump! clicks)
(display out (● clicks count))
(newline out))
(write-line out (greet (lambda () "world")))
(write out
(with-prompt emitting
(lambda ()
(count-down 3)
(Nil))
(lambda (value resume) (Cons value (resume (unit))))))
(newline out)
(let ((left (new Label (text "left")))
(right (new Label (text "right"))))
(swap! left right)
(write-line out (string-append (● left text) " " (● right text))))
(unit))
It prints:
seven squared is 49
5.25
negative
5050
(1 9 25)
15
(12 12)
5
division by zero
(1, 2) (3, 4)
yes no
2
hello, world
(3 2 1)
right left