Show description
Scheme Cheat Sheet | CSE 240
Scheme Cheat Sheet | CSE 240
Scheme Cheat Sheet
Functional Programming in Lisp
Scheme Fundamentals
Key Concepts
Functional Programming: Functions are first-class citizens, can be passed around like data
Prefix Notation: Operator comes first: (+ 1 2) instead of 1 + 2
S-Expressions: Everything is a list in parentheses
Immutability: Data doesn't change; you create new data instead
Recursion: Primary way to loop and iterate
Basic Syntax
; Comments start with semicolon
; Numbers
42
3.14
-17
2/3 ; Rational number
; Booleans
#t ; true
#f ; false
; Strings
"hello world"
"CSE 240"
; Symbols (like atoms in Prolog)
'apple
'foo
'x
; Lists
'(1 2 3 4)
'(a b c)
'() ; empty list
Prefix Notation
; Arithmetic - operator comes first
(+ 2 3) ; => 5
(- 10 4) ; => 6
(* 5 6) ; => 30
(/ 10 3) ; => 10/3 or 3.333...
(quotient 10 3) ; => 3 (integer division)
(remainder 10 3) ; => 1
(expt 2 8) ; => 256 (2^8)
; Multiple arguments
(+ 1 2 3 4 5) ; => 15
(* 2 3 4) ; => 24
; Nested operations
(+ (* 2 3) (- 10 4)) ; => 6 + 6 = 12
(/ (+ 8 4) (- 6 3)) ; => 12 / 3 = 4
Comparison Operators
(= 5 5) ; => #t (numbers only)
(< 3 5) ; => #t
(> 10 4) ; => #t
( #t
(>= 8 3) ; => #t
; General equality
(eq? 'a 'a) ; => #t (pointer equality)
(eqv? 5 5) ; => #t (value equality)
(equal? '(1 2) '(1 2)) ; => #t (structural equality)
; Type predicates
(number? 42) ; => #t
(string? "hello") ; => #t
(list? '(1 2 3)) ; => #t
(null? '()) ; => #t
(pair? '(1 .…
Scheme Cheat Sheet | CSE 240
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scheme Cheat Sheet | CSE 240</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: #0a0a0a;
color: #e0e0e0;
line-height: 1.6;
padding: 40px 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.header {
text-align: center;
margin-bottom: 50px;
padding-bottom: 20px;
border-bottom: 3px solid #4a90e2;
}
.header h1 {
color: #4a90e2;
font-size: 3em;
margin-bottom: 10px;
}
.header p {
color: #888;
font-size: 1.2em;
}
.section {
background: #1a1a1a;
border-radius: 8px;
padding: 30px;
margin-bottom: 30px;
border-left: 4px solid #4a90e2;
}
.section h2 {
color: #4a90e2;
font-size: 2em;
margin-bottom: 20px;
padding-bottom: 10px;
border-bottom: 2px solid #333;
}
.section h3 {
color: #6ba3e8;
font-size: 1.4em;
margin: 25px 0 15px 0;
}
.subsection {
background: #0f0f0f;
padding: 20px;
border-radius: 6px;
margin: 15px 0;
border: 1px solid #333;
}
.subsection h4 {
color: #4a90e2;
font-size: 1.1em;
margin-bottom: 10px;
}
pre {
background: #0a0a0a;
border: 1px solid #444;
border-radius: 6px;
padding: 20px;
overflow-x: auto;
margin: 15px 0;
font-family: 'Courier New', monospace;
}
code {
font-family: 'Courier New', monospace;
font-size: 0.95em;
line-height: 1.5;
color: #e0e0e0;
}
.inline-code {
background: #1a1a1a;
color: #6ba3e8;
padding: 2px 8px;
border-radius: 3px;
font-family: 'Courier New', monospace;
font-size: 0.9em;
}
.tip {
background: #1a2a2a;
border-left: 4px solid #4a90e2;
padding: 15px;
margin: 15px 0;
border-radius: 4px;
}
.tip strong {
color: #4a90e2;
}
.warning {
background: #2a1a1a;
border-left: 4px solid #e24a4a;
padding: 15px;
margin: 15px 0;
border-radius: 4px;
}
.warning strong {
color: #e24a4a;
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
margin: 20px 0;
}
.card {
background: #0f0f0f;
padding: 20px;
border-radius: 6px;
border: 1px solid #333;
}
.card h4 {
color: #6ba3e8;
margin-bottom: 10px;
}
table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
}
th, td {
padding: 12px;
text-align: left;
border: 1px solid #333;
}
th {
background: #0f0f0f;
color: #4a90e2;
font-weight: bold;
}
td {
background: #1a1a1a;
}
tr:hover td {
background: #222;
}
::-webkit-scrollbar {
width: 10px;
height: 10px;
}
::-webkit-scrollbar-track {
background: #1a1a1a;
}
::-webkit-scrollbar-thumb {
background: #4a90e2;
border-radius: 5px;
}
::-webkit-scrollbar-thumb:hover {
background: #6ba3e8;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Scheme Cheat Sheet</h1>
<p>Functional Programming in Lisp</p>
</div>
<!-- BASICS -->
<section class="section">
<h2>Scheme Fundamentals</h2>
<div class="subsection">
<h4>Key Concepts</h4>
<p><strong>Functional Programming:</strong> Functions are first-class citizens, can be passed around like data</p>
<p><strong>Prefix Notation:</strong> Operator comes first: (+ 1 2) instead of 1 + 2</p>
<p><strong>S-Expressions:</strong> Everything is a list in parentheses</p>
<p><strong>Immutability:</strong> Data doesn't change; you create new data instead</p>
<p><strong>Recursion:</strong> Primary way to loop and iterate</p>
</div>
<h3>Basic Syntax</h3>
<pre><code>; Comments start with semicolon
; Numbers
42
3.14
-17
2/3 ; Rational number
; Booleans
#t ; true
#f ; false
; Strings
"hello world"
"CSE 240"
; Symbols (like atoms in Prolog)
'apple
'foo
'x
; Lists
'(1 2 3 4)
'(a b c)
'() ; empty list</code></pre>
<h3>Prefix Notation</h3>
<pre><code>; Arithmetic - operator comes first
(+ 2 3) ; => 5
(- 10 4) ; => 6
(* 5 6) ; => 30
(/ 10 3) ; => 10/3 or 3.333...
(quotient 10 3) ; => 3 (integer division)
(remainder 10 3) ; => 1
(expt 2 8) ; => 256 (2^8)
; Multiple arguments
(+ 1 2 3 4 5) ; => 15
(* 2 3 4) ; => 24
; Nested operations
(+ (* 2 3) (- 10 4)) ; => 6 + 6 = 12
(/ (+ 8 4) (- 6 3)) ; => 12 / 3 = 4</code></pre>
<h3>Comparison Operators</h3>
<pre><code>(= 5 5) ; => #t (numbers only)
(< 3 5) ; => #t
(> 10 4) ; => #t
(<= 5 5) ; => #t
(>= 8 3) ; => #t
; General equality
(eq? 'a 'a) ; => #t (pointer equality)
(eqv? 5 5) ; => #t (value equality)
(equal? '(1 2) '(1 2)) ; => #t (structural equality)
; Type predicates
(number? 42) ; => #t
(string? "hello") ; => #t
(list? '(1 2 3)) ; => #t
(null? '()) ; => #t
(pair? '(1 . 2)) ; => #t</code></pre>
</section>
<!-- DEFINING THINGS -->
<section class="section">
<h2>Defining Variables & Functions</h2>
<h3>Variables</h3>
<pre><code>; Define a variable
(define x 10)
(define name "Bob")
(define pi 3.14159)
; Using variables
(+ x 5) ; => 15
(* pi 2) ; => 6.28318</code></pre>
<h3>Functions</h3>
<pre><code>; Basic function definition
(define (square x)
(* x x))
(square 5) ; => 25
; Multiple parameters
(define (add-three a b c)
(+ a b c))
(add-three 1 2 3) ; => 6
; Function with multiple expressions (last one is returned)
(define (greet name)
(display "Hello, ")
(display name)
(newline)
"Done!") ; This is returned
; Recursive function
(define (factorial n)
(if (<= n 1)
1
(* n (factorial (- n 1)))))
(factorial 5) ; => 120</code></pre>
<h3>Lambda (Anonymous Functions)</h3>
<pre><code>; Lambda creates unnamed functions
(lambda (x) (* x x))
; Using lambda directly
((lambda (x) (* x x)) 5) ; => 25
; Lambda with multiple parameters
((lambda (x y) (+ x y)) 3 4) ; => 7
; Storing lambda in a variable
(define square (lambda (x) (* x x)))
(square 6) ; => 36
; Lambda in higher-order functions
(map (lambda (x) (* x 2)) '(1 2 3 4)) ; => (2 4 6 8)</code></pre>
</section>
<!-- CONDITIONALS -->
<section class="section">
<h2>Conditionals</h2>
<h3>if</h3>
<pre><code>; Syntax: (if condition then-expr else-expr)
(if (> 5 3)
"yes"
"no") ; => "yes"
(define (abs x)
(if (< x 0)
(- x)
x))
(abs -5) ; => 5
(abs 3) ; => 3
; Nested if
(define (sign x)
(if (> x 0)
"positive"
(if (< x 0)
"negative"
"zero")))</code></pre>
<h3>cond</h3>
<pre><code>; cond is like switch/case - cleaner for multiple conditions
(define (grade score)
(cond
((>= score 90) 'A)
((>= score 80) 'B)
((>= score 70) 'C)
((>= score 60) 'D)
(else 'F)))
(grade 85) ; => B
; Another example
(define (describe-number n)
(cond
((< n 0) "negative")
((= n 0) "zero")
((< n 10) "small positive")
((< n 100) "medium positive")
(else "large positive")))
(describe-number 42) ; => "medium positive"</code></pre>
<h3>Boolean Logic</h3>
<pre><code>; and - returns #f if any are false
(and #t #t #t) ; => #t
(and #t #f #t) ; => #f
; or - returns #t if any are true
(or #f #f #t) ; => #t
(or #f #f #f) ; => #f
; not - negation
(not #t) ; => #f
(not #f) ; => #t
; Practical example
(define (in-range? x low high)
(and (>= x low) (<= x high)))
(in-range? 5 1 10) ; => #t
(in-range? 15 1 10) ; => #f</code></pre>
</section>
<!-- LISTS -->
<section class="section">
<h2>Lists</h2>
<div class="tip">
<strong>Lists are fundamental in Scheme!</strong> Almost everything works with lists.
</div>
<h3>Creating Lists</h3>
<pre><code>; Quote syntax
'(1 2 3 4)
'(a b c d)
'() ; empty list
; List function
(list 1 2 3 4) ; => (1 2 3 4)
(list 'a 'b 'c) ; => (a b c)
; cons - builds a list by adding to front
(cons 1 '(2 3 4)) ; => (1 2 3 4)
(cons 'a '(b c)) ; => (a b c)
(cons 1 '()) ; => (1)</code></pre>
<h3>List Operations</h3>
<pre><code>; car - get first element (head)
(car '(1 2 3)) ; => 1
(car '(a b c)) ; => a
; cdr - get rest of list (tail)
(cdr '(1 2 3)) ; => (2 3)
(cdr '(a b c)) ; => (b c)
; Combining car and cdr
(cadr '(1 2 3)) ; => 2 (car of cdr)
(caddr '(1 2 3)) ; => 3 (car of cdr of cdr)
; null? - check if empty
(null? '()) ; => #t
(null? '(1 2)) ; => #f
; length
(length '(1 2 3 4)) ; => 4
(length '()) ; => 0
; append - combine lists
(append '(1 2) '(3 4)) ; => (1 2 3 4)
(append '(a b) '(c d) '(e f)) ; => (a b c d e f)
; reverse
(reverse '(1 2 3 4)) ; => (4 3 2 1)
; list-ref - get nth element (0-indexed)
(list-ref '(a b c d) 0) ; => a
(list-ref '(a b c d) 2) ; => c</code></pre>
<h3>List Recursion Patterns</h3>
<pre><code>; Sum of list
(define (sum lst)
(if (null? lst)
0
(+ (car lst) (sum (cdr lst)))))
(sum '(1 2 3 4 5)) ; => 15
; Length (recursive)
(define (my-length lst)
(if (null? lst)
0
(+ 1 (my-length (cdr lst)))))
; Find maximum
(define (my-max lst)
(if (null? (cdr lst))
(car lst)
(let ((rest-max (my-max (cdr lst))))
(if (> (car lst) rest-max)
(car lst)
rest-max))))
(my-max '(3 7 2 9 1)) ; => 9
; Member check
(define (my-member? item lst)
(cond
((null? lst) #f)
((equal? item (car lst)) #t)
(else (my-member? item (cdr lst)))))
(my-member? 3 '(1 2 3 4)) ; => #t
(my-member? 5 '(1 2 3 4)) ; => #f
; Remove duplicates
(define (remove-dups lst)
(cond
((null? lst) '())
((member (car lst) (cdr lst))
(remove-dups (cdr lst)))
(else
(cons (car lst) (remove-dups (cdr lst))))))</code></pre>
</section>
<!-- HIGHER-ORDER FUNCTIONS -->
<section class="section">
<h2>Higher-Order Functions</h2>
<div class="subsection">
<h4>What are Higher-Order Functions?</h4>
<p>Functions that take other functions as arguments or return functions</p>
</div>
<h3>map</h3>
<pre><code>; Apply function to each element
(map square '(1 2 3 4)) ; => (1 4 9 16)
(map (lambda (x) (* x 2)) '(1 2 3)) ; => (2 4 6)
(map abs '(-1 2 -3 4)) ; => (1 2 3 4)
; Map with multiple lists
(map + '(1 2 3) '(4 5 6)) ; => (5 7 9)
(map * '(1 2 3) '(4 5 6)) ; => (4 10 18)</code></pre>
<h3>filter</h3>
<pre><code>; Keep only elements that satisfy predicate
(filter even? '(1 2 3 4 5 6)) ; => (2 4 6)
(filter odd? '(1 2 3 4 5 6)) ; => (1 3 5)
(filter (lambda (x) (> x 5)) '(1 3 7 4 9 2)) ; => (7 9)
; Custom filter example
(define (positive-only lst)
(filter (lambda (x) (> x 0)) lst))
(positive-only '(-3 5 -1 8 -9 2)) ; => (5 8 2)</code></pre>
<h3>fold/reduce</h3>
<pre><code>; foldr - fold from the right
(foldr + 0 '(1 2 3 4)) ; => 10
(foldr * 1 '(1 2 3 4)) ; => 24
(foldr cons '() '(1 2 3)) ; => (1 2 3)
; foldl - fold from the left (more efficient)
(foldl + 0 '(1 2 3 4)) ; => 10
(foldl cons '() '(1 2 3)) ; => (3 2 1)
; Using fold for complex operations
(define (my-sum lst)
(foldr + 0 lst))
(define (my-product lst)
(foldr * 1 lst))
(define (count-if pred lst)
(foldr (lambda (x acc)
(if (pred x)
(+ 1 acc)
acc))
0
lst))
(count-if even? '(1 2 3 4 5 6)) ; => 3</code></pre>
<h3>apply</h3>
<pre><code>; Apply function to list as separate arguments
(apply + '(1 2 3 4)) ; => 10
(apply max '(3 7 2 9 1)) ; => 9
(apply string-append '("hello" " " "world")) ; => "hello world"</code></pre>
<h3>Creating Higher-Order Functions</h3>
<pre><code>; Function that returns a function
(define (make-adder n)
(lambda (x) (+ x n)))
(define add5 (make-adder 5))
(add5 10) ; => 15
(define add100 (make-adder 100))
(add100 10) ; => 110
; Function that takes a function
(define (twice f x)
(f (f x)))
(twice square 3) ; => 81 (square of square)
(twice (lambda (x) (+ x 1)) 5) ; => 7
; Compose functions
(define (compose f g)
(lambda (x) (f (g x))))
(define add1-then-square (compose square (lambda (x) (+ x 1))))
(add1-then-square 4) ; => 25</code></pre>
</section>
<!-- LET BINDINGS -->
<section class="section">
<h2>Let Bindings</h2>
<h3>let</h3>
<pre><code>; Create local variables
(let ((x 5)
(y 10))
(+ x y)) ; => 15
; More complex example
(define (quadratic a b c x)
(let ((discriminant (- (* b b) (* 4 a c))))
(+ (* a x x) (* b x) c)))
; Let with computation
(let ((x (+ 2 3))
(y (* 4 5)))
(+ x y)) ; => 25
; Practical example
(define (distance x1 y1 x2 y2)
(let ((dx (- x2 x1))
(dy (- y2 y1)))
(sqrt (+ (* dx dx) (* dy dy)))))</code></pre>
<h3>let*</h3>
<pre><code>; let* allows later bindings to use earlier ones
(let* ((x 5)
(y (* x 2))
(z (+ x y)))
z) ; => 15
; This would NOT work with regular let:
; (let ((x 5)
; (y (* x 2))) ; x not in scope yet!
; ...)
; Practical example
(define (compute-grade homework midterm final)
(let* ((hw-weight 0.3)
(mid-weight 0.3)
(final-weight 0.4)
(weighted-hw (* homework hw-weight))
(weighted-mid (* midterm mid-weight))
(weighted-final (* final final-weight))
(total (+ weighted-hw weighted-mid weighted-final)))
total))</code></pre>
<h3>letrec</h3>
<pre><code>; letrec allows recursive definitions
(letrec ((fact (lambda (n)
(if (<= n 1)
1
(* n (fact (- n 1)))))))
(fact 5)) ; => 120
; Mutual recursion
(letrec ((even? (lambda (n)
(if (= n 0)
#t
(odd? (- n 1)))))
(odd? (lambda (n)
(if (= n 0)
#f
(even? (- n 1))))))
(even? 10)) ; => #t</code></pre>
</section>
<!-- RECURSION PATTERNS -->
<section class="section">
<h2>Common Recursion Patterns</h2>
<h3>Direct Recursion</h3>
<pre><code>; Factorial
(define (factorial n)
(if (<= n 1)
1
(* n (factorial (- n 1)))))
; Fibonacci
(define (fib n)
(cond
((= n 0) 0)
((= n 1) 1)
(else (+ (fib (- n 1))
(fib (- n 2))))))
; Power
(define (power base exp)
(if (= exp 0)
1
(* base (power base (- exp 1)))))
; GCD (Euclid's algorithm)
(define (gcd a b)
(if (= b 0)
a
(gcd b (remainder a b))))</code></pre>
<h3>Tail Recursion</h3>
<pre><code>; Tail recursive factorial (more efficient)
(define (factorial-tail n)
(define (fact-helper n acc)
(if (<= n 1)
acc
(fact-helper (- n 1) (* n acc))))
(fact-helper n 1))
; Tail recursive sum
(define (sum-tail lst)
(define (sum-helper lst acc)
(if (null? lst)
acc
(sum-helper (cdr lst) (+ acc (car lst)))))
(sum-helper lst 0))
; Tail recursive reverse
(define (reverse-tail lst)
(define (rev-helper lst acc)
(if (null? lst)
acc
(rev-helper (cdr lst) (cons (car lst) acc))))
(rev-helper lst '()))
(reverse-tail '(1 2 3 4)) ; => (4 3 2 1)</code></pre>
<h3>List Building Patterns</h3>
<pre><code>; Map implementation
(define (my-map f lst)
(if (null? lst)
'()
(cons (f (car lst))
(my-map f (cdr lst)))))
; Filter implementation
(define (my-filter pred lst)
(cond
((null? lst) '())
((pred (car lst))
(cons (car lst) (my-filter pred (cdr lst))))
(else (my-filter pred (cdr lst)))))
; Range (like Python's range)
(define (range start end)
(if (>= start end)
'()
(cons start (range (+ start 1) end))))
(range 1 6) ; => (1 2 3 4 5)
; Repeat element n times
(define (repeat x n)
(if (<= n 0)
'()
(cons x (repeat x (- n 1)))))
(repeat 'a 5) ; => (a a a a a)</code></pre>
</section>
<!-- PRACTICAL EXAMPLES -->
<section class="section">
<h2>Practical Examples</h2>
<h3>List Processing</h3>
<pre><code>; Flatten nested list
(define (flatten lst)
(cond
((null? lst) '())
((list? (car lst))
(append (flatten (car lst))
(flatten (cdr lst))))
(else
(cons (car lst) (flatten (cdr lst))))))
(flatten '(1 (2 3) ((4) 5))) ; => (1 2 3 4 5)
; Zip two lists together
(define (zip lst1 lst2)
(if (or (null? lst1) (null? lst2))
'()
(cons (list (car lst1) (car lst2))
(zip (cdr lst1) (cdr lst2)))))
(zip '(1 2 3) '(a b c)) ; => ((1 a) (2 b) (3 c))
; Take first n elements
(define (take n lst)
(if (or (<= n 0) (null? lst))
'()
(cons (car lst) (take (- n 1) (cdr lst)))))
(take 3 '(1 2 3 4 5)) ; => (1 2 3)
; Drop first n elements
(define (drop n lst)
(if (or (<= n 0) (null? lst))
lst
(drop (- n 1) (cdr lst))))
(drop 2 '(1 2 3 4 5)) ; => (3 4 5)</code></pre>
<h3>Quicksort</h3>
<pre><code>(define (quicksort lst)
(if (null? lst)
'()
(let ((pivot (car lst))
(rest (cdr lst)))
(append
(quicksort (filter (lambda (x) (< x pivot)) rest))
(list pivot)
(quicksort (filter (lambda (x) (>= x pivot)) rest))))))
(quicksort '(3 7 2 9 1 5 8)) ; => (1 2 3 5 7 8 9)</code></pre>
<h3>Tree Operations</h3>
<pre><code>; Binary tree: (value left right) or ()
; Count nodes
(define (count-nodes tree)
(if (null? tree)
0
(+ 1
(count-nodes (cadr tree))
(count-nodes (caddr tree)))))
; Tree height
(define (tree-height tree)
(if (null? tree)
0
(+ 1 (max (tree-height (cadr tree))
(tree-height (caddr tree))))))
; Sum all values in tree
(define (sum-tree tree)
(if (null? tree)
0
(+ (car tree)
(sum-tree (cadr tree))
(sum-tree (caddr tree)))))
; Example tree
(define my-tree '(5 (3 (1 () ()) (4 () ())) (8 (6 () ()) (9 () ()))))
(count-nodes my-tree) ; => 7
(tree-height my-tree) ; => 3
(sum-tree my-tree) ; => 36</code></pre>
<h3>String Processing</h3>
<pre><code>; String to list of characters
(string->list "hello") ; => (#\h #\e #\l #\l #\o)
; List of characters to string
(list->string '(#\h #\i)) ; => "hi"
; Reverse a string
(define (reverse-string s)
(list->string (reverse (string->list s))))
(reverse-string "hello") ; => "olleh"
; Count vowels
(define (vowel? c)
(member c '(#\a #\e #\i #\o #\u #\A #\E #\I #\O #\U)))
(define (count-vowels s)
(length (filter vowel? (string->list s))))
(count-vowels "hello world") ; => 3</code></pre>
</section>
<!-- TIPS -->
<section class="section">
<h2>Important Tips & Gotchas</h2>
<div class="tip">
<strong>Always include base cases:</strong> Every recursive function needs a stopping condition
</div>
<div class="tip">
<strong>Use tail recursion when possible:</strong> It's more efficient and won't blow the stack
</div>
<div class="tip">
<strong>Quote your lists:</strong> '(1 2 3) not (1 2 3) - without quote Scheme tries to evaluate it
</div>
<div class="warning">
<strong>car and cdr fail on empty lists:</strong> Always check (null? lst) first
</div>
<div class="tip">
<strong>Use let for readability:</strong> Name intermediate values instead of nesting deeply
</div>
<div class="tip">
<strong>map, filter, fold are your friends:</strong> Learn to think in terms of transformations
</div>
<div class="warning">
<strong>Be careful with eq? vs equal?:</strong> eq? checks pointer equality, equal? checks structural equality
</div>
<div class="tip">
<strong>Think recursively:</strong> Break problems into base case + recursive case
</div>
<h3>Common Scheme Idioms</h3>
<pre><code>; Process each element and build new list
(define (process-list lst)
(if (null? lst)
'()
(cons (process-item (car lst))
(process-list (cdr lst)))))
; Filter based on condition
(define (filter-list lst)
(cond
((null? lst) '())
((condition? (car lst))
(cons (car lst) (filter-list (cdr lst))))
(else (filter-list (cdr lst)))))
; Accumulate with helper
(define (accumulate lst)
(define (helper lst acc)
(if (null? lst)
acc
(helper (cdr lst) (combine (car lst) acc))))
(helper lst initial-value))</code></pre>
<h3>Debugging Tips</h3>
<pre><code>; Use display to print debug info
(define (debug-factorial n)
(display "Computing factorial of: ")
(display n)
(newline)
(if (<= n 1)
1
(* n (debug-factorial (- n 1)))))
; Trace recursion depth
(define (trace-depth depth n)
(display "Depth: ")
(display depth)
(display " n: ")
(display n)
(newline)
(if (<= n 0)
0
(+ n (trace-depth (+ depth 1) (- n 1)))))</code></pre>
</section>
<!-- QUICK REFERENCE -->
<section class="section">
<h2>Quick Reference</h2>
<div class="grid">
<div class="card">
<h4>List Operations</h4>
<pre><code>car ; first
cdr ; rest
cons ; prepend
null? ; empty?
length ; size
append ; concatenate
reverse ; reverse
list-ref ; get nth
member ; contains?</code></pre>
</div>
<div class="card">
<h4>Higher-Order</h4>
<pre><code>map ; transform
filter ; select
foldr ; reduce right
foldl ; reduce left
apply ; call with list
lambda ; anonymous fn</code></pre>
</div>
<div class="card">
<h4>Arithmetic</h4>
<pre><code>+ - * /
quotient ; int div
remainder ; modulo
expt ; power
sqrt ; square root
abs ; absolute
max min ; extremes</code></pre>
</div>
<div class="card">
<h4>Comparison</h4>
<pre><code>= ; numeric eq
< > <= >=
eq? ; pointer eq
eqv? ; value eq
equal? ; structural eq
and or not</code></pre>
</div>
</div>
</section>
</div>
</body>
</html>