Scheme is a minimal dialect of LISP, developed in the 1970s. While it supports both functional and procedural paradigms, Scheme is mainly functional — built for learning core concepts of programming languages.
Procedural programming focuses on statements. Functional programming focuses on expressions.
In this tutorial we use Racket, a dialect of Scheme. You can follow along with DrRacket.
Basic primitives
- Booleans:
#t,#f - Numbers:
123,3/4 - Strings:
"Hello world!" - Lists:
(1 2 3 4 5) - Pairs:
(x . z)
In Scheme, procedure calls look like (f x y) instead of f(x, y).
Writing procedures
The + is a built-in procedure. To add two numbers: (+ 1 2) yields 3.
We can create our own with lambda:
(define add2 (lambda (x) (+ x 2)))
(add2 1) ; => 3
Or compute a cube:
(define cube (lambda (x) (* x x x)))
(cube 3) ; => 27
Recursion
The classic example — factorial:
(define (factorial n)
(if (= n 0)
1
(* n (factorial (- n 1)))))
if is a keyword: (if cond a b) executes a if the condition is true, otherwise b.
Code available on GitHub.
With gratitude to professors Matteo Pradella and Michele Chiari who taught Principles of Programming Languages at PoliMi.