paulgorman.org

Scheme Programming Notes

Chicken

There are a number of good implementations of Scheme. I'm using Chicken Scheme. It has a manual.

Invoke the REPL with csi. Within the REPL, you can load a file like (load "test.scm").

Run a script like csi -s test.scm.

Also see csi -help.

Chicken can compile executables like csc -o test test.scm.

Chicken modules are called eggs. Install an egg like $ chicken-install egg-name. The module can then be used with (use egg-name).

Hello, world!

; A comment.
(define (hello)
  (display "Hello, world!")
  (newline))

(hello)

Types

(boolean? #t)    ; True
(boolean? "Hello, World!    ; False
(not #f)    ; True
(not #t)    ; False
(not "Hello, World")    ; False

(number? 42)    ; True
(number? #t)    ; False
(integer? 10/3)    ; False

(eqv? 42 42)    ; True
(eqv? 42 "Hello, world!")   ; False

(= 42 42)    ; True.
(= 42 #f)    ; Error. Unlike eqv?, = is a numeric-only operator.

(+ 2 2)    ; 4

(max 1 2 3 4)    ; 4
(mim 1 2 3 4)    ; 1

(abs -2)    ; 2

Symbols

(quote abc)    ; abc
'abc    ; Shorthand for (quote abc)

(define x 3)    ; Define variable
(set! x 9)    ; Set new value

String

(define z "Hello")
(string-append z ", world!")    ; "Hello, world!"
(string? z)    ; #t

Links