The VikiD Truth

Knowledge base / Computing / Lisp family

Read This! | Picolisp | Picolisp Machine | Pil Sources | Linux | BASH | C-Programmming | Javascipt | Python | Racket | Scheme | Operating Systems | AssemblyLanguage | Computer Security | Firewalls | Exploitation | Social Engineering | Metasploit | Emacs | vim | Pharo Smalltalk | Databases

Scheme

I am partial to scheme, I always really appreciated it.

   (define (max-of-list lst)
     (foldr max (first lst) (rest lst)))

That little function says why. It finds the largest number in a list by folding max across it, in one line, with no loop counter and no temporary variable. That is Scheme’s whole character: small, sharp ideas that combine.

What it is, in plain words

Here is the truth about Scheme: it is the Lisp that was designed by subtraction. Guy Steele and Gerald Sussman created it at MIT in 1975 to understand the actor model, and found that a tiny Lisp with lexical scope and first-class functions already did everything they needed.

Scheme gives you:

The standards are the RnRS reports. R5RS (1998) is tiny and still widely taught. R6RS (2007, see below) is larger and stricter. R7RS-small (2013) is what most implementations target today; R7RS-large is being built piece by piece through SRFIs (Scheme Requests for Implementation).

Why it matters

An everyday analogy: Lego Technic

Scheme is a small box of Lego Technic pieces: beams, pins, gears and axles. There is no pre-moulded car body in the box. But because the pieces connect in every direction, you can build a gearbox, a crane or a car, and you understand exactly how each one works because you built it.

A hands-on example

Install a Scheme on Vikix: xbps-install -S chez-scheme (R6RS, very fast) or guile (R7RS mostly, GNU), or use Racket’s #lang r7rs.

;; Tail recursion: this loop runs in constant stack space
(define (sum-to n)
  (let loop ((i 0) (acc 0))
    (if (> i n)
        acc
        (loop (+ i 1) (+ acc i)))))

(sum-to 1000000)          ; => 500000500000

;; Closures: a counter that remembers its own state
(define (make-counter)
  (let ((n 0))
    (lambda () (set! n (+ n 1)) n)))

(define c (make-counter))
(c) (c)                   ; => 2

;; Higher-order functions
(map (lambda (x) (* x x)) '(1 2 3 4))    ; => (1 4 9 16)

;; The fold from the top of the page, in portable R7RS
(define (max-of-list lst)
  (let loop ((best (car lst)) (rest (cdr lst)))
    (if (null? rest)
        best
        (loop (max best (car rest)) (cdr rest)))))
(max-of-list '(3 9 2 7))  ; => 9

Note foldr is a Racket name; portable Scheme gets folds from SRFI 1 (fold, fold-right). The PicoLisp version of the same idea is just (apply max '(3 9 2 7)).

Common misconceptions

Connections

R6RS - Revision 6 Scheme Standard

Programming languages should be designed not by piling feature on top of feature, but by removing the weaknesses and restrictions that make additional features appear necessary. Scheme demonstrates that a very small number of rules for forming expressions, with no restrictions on how they are composed, suffice to form a practical and efficient programming language that is flexible enough to support most of the major … Read more.

Last edited · 16 revisions

Tags: #Computing