Learn SCHEME with Real Code Examples
Updated Nov 20, 2025
Code Sample Descriptions
1
Scheme Counter and Theme Toggle
(define count 0)
(define isDark #f)
(define (updateUI)
(display "Counter: ") (display count) (newline)
(display "Theme: ") (display (if isDark "Dark" "Light")) (newline))
(define (increment)
(set! count (+ count 1))
(updateUI))
(define (decrement)
(set! count (- count 1))
(updateUI))
(define (reset)
(set! count 0)
(updateUI))
(define (toggleTheme)
(set! isDark (not isDark))
(updateUI))
; Simulate actions
(updateUI)
(increment)
(increment)
(toggleTheme)
(decrement)
(reset)
Demonstrates a simple counter with theme toggling using Scheme variables and console output.
2
Scheme Simple Addition
(define (add a b) (+ a b))
(display (add 10 20)) (newline)
Adds two numbers and prints the result.
3
Scheme Factorial
(define (factorial n)
(if (<= n 1)
1
(* n (factorial (- n 1)))))
(display (factorial 5)) (newline)
Calculates factorial recursively.
4
Scheme Fibonacci Sequence
(define (fib n)
(if (< n 2) n
(+ (fib (- n 1)) (fib (- n 2)))))
(for-each (lambda (i) (display (fib i)) (newline)) (iota 10))
Generates first 10 Fibonacci numbers.
5
Scheme Max of Two Numbers
(define (max a b) (if (> a b) a b))
(display (max 10 20)) (newline)
Finds the maximum of two numbers.
6
Scheme List Sum
(define (sum-list lst)
(if (null? lst) 0
(+ (car lst) (sum-list (cdr lst)))))
(display (sum-list '(1 2 3 4 5))) (newline)
Sums elements of a list recursively.
7
Scheme Even Numbers Filter
(define (print-even lst)
(for-each (lambda (x) (if (even? x) (display x) (void)) (newline)) lst))
(print-even '(1 2 3 4 5))
Prints even numbers from a list.
8
Scheme Conditional Counter Increment
(define count 3)
(if (< count 5) (set! count (+ count 1)))
(display count) (newline)
Increment counter only if less than 5.
9
Scheme Resettable Counter
(define count 0)
(set! count (+ count 1))
(set! count (+ count 1))
(display count) (newline)
(set! count 0)
(display count) (newline)
Counter that increments and can be reset.
10
Scheme Theme Toggle Only
(define isDark #f)
(display (if isDark "Dark" "Light")) (newline)
(set! isDark (not isDark))
(display (if isDark "Dark" "Light")) (newline)
(set! isDark (not isDark))
(display (if isDark "Dark" "Light")) (newline)
Toggles theme multiple times.