Show description
Scheme & Functional Programming Cheat Sheet
Scheme & Functional Programming Cheat Sheet
Scheme & Functional Programming Cheat Sheet
Basic Syntax & Notation
Prefix Notation
;; Infix vs Prefix
;; Infix: 2 + 3 * 4
;; Prefix: (+ 2 (* 3 4))
;; All operations are functions
(+ 1 2 3 4) ;; → 10
(* 2 3 4) ;; → 24
(- 10 3) ;; → 7
(/ 20 4) ;; → 5
Basic Forms
;; Numbers
42, 3.14, -17, 1/3
;; Booleans
#t, #f
;; Characters
#\a, #\b, #\space, #\newline
;; Strings
"Hello World"
;; Symbols
'hello, 'world, '+
Core Procedures
Procedure
Description
Example
define
Create variables/procedures
(define x 5)
if
Conditional expression
(if (> x 0) "pos" "neg")
cond
Multiple conditions
(cond [(> x 0) "pos"] [else "neg"])
and
Logical AND
(and #t #f) → #f
or
Logical OR
(or #t #f) → #t
not
Logical NOT
(not #t) → #f
abs
Absolute value
(abs -5) → 5
odd?
Check if odd
(odd? 3) → #t
even?
Check if even
(even? 4) → #t
Defining Procedures
;; Basic procedure definition
(define (procedure-name param1 param2)
body-expression)
;; Example: Add procedure
(define (add x y)
(+ x y))
;; Usage
(add 5 3) ;; → 8
;; Lambda (anonymous functions)
(lambda (x y) (+ x y))
((lambda (x y) (+ x y)) 5 3) ;; → 8
The Fantastic Recursive Approach
Four Steps to Recursive Solutions:
Size-n Problem: What are we trying to solve?
Stopping Conditions: When do we stop recursing?
Size-m Problems: What smaller problems do we solve?
Construction: How do we build the solution from smaller parts?
Factorial Example
(define (factorial n)
;; 1. Size-n problem: factorial of n
(if (
Scheme & Functional Programming Cheat Sheet
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scheme & Functional Programming Cheat Sheet</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Consolas', 'Monaco', monospace;
background: linear-gradient(135deg, #2c2c2c 0%, #1a1a1a 100%);
color: #e0e0e0;
line-height: 1.6;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
gap: 20px;
}
h1 {
text-align: center;
color: #ff6b35;
font-size: 2.5em;
margin-bottom: 30px;
text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
grid-column: 1 / -1;
}
.section {
background: linear-gradient(145deg, #3a3a3a, #2a2a2a);
border-radius: 10px;
padding: 20px;
box-shadow: 0 8px 16px rgba(0,0,0,0.3);
border: 1px solid #4a4a4a;
}
h2 {
color: #ff6b35;
border-bottom: 2px solid #ff6b35;
padding-bottom: 10px;
margin-bottom: 15px;
font-size: 1.4em;
}
h3 {
color: #ffab00;
margin: 15px 0 10px 0;
font-size: 1.1em;
}
.code {
background: #1e1e1e;
border: 1px solid #555;
border-radius: 5px;
padding: 10px;
margin: 10px 0;
font-family: 'Consolas', monospace;
overflow-x: auto;
color: #e0e0e0;
}
.highlight {
color: #ff6b35;
font-weight: bold;
}
.keyword {
color: #ffab00;
}
.comment {
color: #888;
font-style: italic;
}
ul, ol {
margin-left: 20px;
margin-bottom: 10px;
}
li {
margin-bottom: 5px;
}
.two-column {
grid-column: span 2;
}
.table {
width: 100%;
border-collapse: collapse;
margin: 10px 0;
}
.table th, .table td {
border: 1px solid #555;
padding: 8px;
text-align: left;
}
.table th {
background: #ff6b35;
color: #1a1a1a;
}
.table tr:nth-child(even) {
background: #2a2a2a;
}
.warning {
background: rgba(255, 107, 53, 0.1);
border-left: 4px solid #ff6b35;
padding: 10px;
margin: 10px 0;
}
</style>
</head>
<body>
<div class="container">
<h1>Scheme & Functional Programming Cheat Sheet</h1>
<div class="section">
<h2>Basic Syntax & Notation</h2>
<h3>Prefix Notation</h3>
<div class="code">
<span class="comment">;; Infix vs Prefix</span>
<span class="comment">;; Infix: 2 + 3 * 4</span>
<span class="comment">;; Prefix: (+ 2 (* 3 4))</span>
<span class="comment">;; All operations are functions</span>
(+ 1 2 3 4) <span class="comment">;; → 10</span>
(* 2 3 4) <span class="comment">;; → 24</span>
(- 10 3) <span class="comment">;; → 7</span>
(/ 20 4) <span class="comment">;; → 5</span>
</div>
<h3>Basic Forms</h3>
<div class="code">
<span class="comment">;; Numbers</span>
42, 3.14, -17, 1/3
<span class="comment">;; Booleans</span>
#t, #f
<span class="comment">;; Characters</span>
#\a, #\b, #\space, #\newline
<span class="comment">;; Strings</span>
"Hello World"
<span class="comment">;; Symbols</span>
'hello, 'world, '+
</div>
</div>
<div class="section">
<h2>Core Procedures</h2>
<table class="table">
<tr><th>Procedure</th><th>Description</th><th>Example</th></tr>
<tr><td><span class="keyword">define</span></td><td>Create variables/procedures</td><td>(define x 5)</td></tr>
<tr><td><span class="keyword">if</span></td><td>Conditional expression</td><td>(if (> x 0) "pos" "neg")</td></tr>
<tr><td><span class="keyword">cond</span></td><td>Multiple conditions</td><td>(cond [(> x 0) "pos"] [else "neg"])</td></tr>
<tr><td><span class="keyword">and</span></td><td>Logical AND</td><td>(and #t #f) → #f</td></tr>
<tr><td><span class="keyword">or</span></td><td>Logical OR</td><td>(or #t #f) → #t</td></tr>
<tr><td><span class="keyword">not</span></td><td>Logical NOT</td><td>(not #t) → #f</td></tr>
<tr><td><span class="keyword">abs</span></td><td>Absolute value</td><td>(abs -5) → 5</td></tr>
<tr><td><span class="keyword">odd?</span></td><td>Check if odd</td><td>(odd? 3) → #t</td></tr>
<tr><td><span class="keyword">even?</span></td><td>Check if even</td><td>(even? 4) → #t</td></tr>
</table>
</div>
<div class="section">
<h2>Defining Procedures</h2>
<div class="code">
<span class="comment">;; Basic procedure definition</span>
(<span class="keyword">define</span> (<span class="highlight">procedure-name</span> param1 param2)
body-expression)
<span class="comment">;; Example: Add procedure</span>
(<span class="keyword">define</span> (<span class="highlight">add</span> x y)
(+ x y))
<span class="comment">;; Usage</span>
(add 5 3) <span class="comment">;; → 8</span>
<span class="comment">;; Lambda (anonymous functions)</span>
(<span class="keyword">lambda</span> (x y) (+ x y))
((lambda (x y) (+ x y)) 5 3) <span class="comment">;; → 8</span>
</div>
</div>
<div class="section two-column">
<h2>The Fantastic Recursive Approach</h2>
<div class="warning">
<strong>Four Steps to Recursive Solutions:</strong>
<ol>
<li><strong>Size-n Problem:</strong> What are we trying to solve?</li>
<li><strong>Stopping Conditions:</strong> When do we stop recursing?</li>
<li><strong>Size-m Problems:</strong> What smaller problems do we solve?</li>
<li><strong>Construction:</strong> How do we build the solution from smaller parts?</li>
</ol>
</div>
<h3>Factorial Example</h3>
<div class="code">
(<span class="keyword">define</span> (<span class="highlight">factorial</span> n)
<span class="comment">;; 1. Size-n problem: factorial of n</span>
(<span class="keyword">if</span> (<= n 1)
<span class="comment">;; 2. Stopping condition: n <= 1, return 1</span>
1
<span class="comment">;; 3. Size-m problem: factorial of (n-1)</span>
<span class="comment">;; 4. Construction: n * factorial(n-1)</span>
(* n (factorial (- n 1)))))
<span class="comment">;; Test cases</span>
(factorial 5) <span class="comment">;; → 120</span>
(factorial 0) <span class="comment">;; → 1</span>
</div>
<h3>Fibonacci Example</h3>
<div class="code">
(<span class="keyword">define</span> (<span class="highlight">fib</span> n)
<span class="comment">;; 1. Size-n problem: nth Fibonacci number</span>
(<span class="keyword">cond</span>
<span class="comment">;; 2. Stopping conditions</span>
[(<= n 0) 0]
[(= n 1) 1]
<span class="comment">;; 3&4. Size-m problems and construction</span>
[<span class="keyword">else</span> (+ (fib (- n 1)) (fib (- n 2)))]))
<span class="comment">;; Test cases</span>
(fib 0) <span class="comment">;; → 0</span>
(fib 9) <span class="comment">;; → 34</span>
</div>
</div>
<div class="section">
<h2>Data Types & Predicates</h2>
<table class="table">
<tr><th>Type</th><th>Predicate</th><th>Example</th></tr>
<tr><td>Number</td><td>number?</td><td>(number? 42) → #t</td></tr>
<tr><td>String</td><td>string?</td><td>(string? "hi") → #t</td></tr>
<tr><td>Symbol</td><td>symbol?</td><td>(symbol? 'x) → #t</td></tr>
<tr><td>Character</td><td>char?</td><td>(char? #\a) → #t</td></tr>
<tr><td>Boolean</td><td>boolean?</td><td>(boolean? #t) → #t</td></tr>
<tr><td>List</td><td>list?</td><td>(list? '(1 2 3)) → #t</td></tr>
<tr><td>Null</td><td>null?</td><td>(null? '()) → #t</td></tr>
</table>
</div>
<div class="section">
<h2>Input/Output</h2>
<div class="code">
<span class="comment">;; Reading input</span>
(<span class="keyword">read</span>) <span class="comment">;; Read from keyboard</span>
<span class="comment">;; Example: Read and square</span>
(<span class="keyword">define</span> (<span class="highlight">read-for-square</span>)
(<span class="keyword">let</span> ([input (<span class="keyword">read</span>)])
(* input input)))
<span class="comment">;; Multiple reads</span>
(* (<span class="keyword">read</span>) (<span class="keyword">read</span>)) <span class="comment">;; Multiply two inputs</span>
</div>
<div class="warning">
<strong>Note:</strong> Avoid display, write, and begin forms in pure functional code
</div>
</div>
<div class="section">
<h2>Common Recursive Patterns</h2>
<h3>Linear Recursion</h3>
<div class="code">
<span class="comment">;; Sum of odd numbers up to max</span>
(<span class="keyword">define</span> (<span class="highlight">sum-odds</span> max)
(<span class="keyword">cond</span>
[(<= max 0) 0]
[(<span class="keyword">odd?</span> max) (+ max (sum-odds (- max 2)))]
[<span class="keyword">else</span> (sum-odds (- max 1))]))
</div>
<h3>Tree Recursion</h3>
<div class="code">
<span class="comment">;; Already shown with Fibonacci</span>
<span class="comment">;; Two recursive calls create tree structure</span>
</div>
<h3>Tail Recursion (with helpers)</h3>
<div class="code">
(<span class="keyword">define</span> (<span class="highlight">factorial-tail</span> n)
(<span class="keyword">define</span> (<span class="highlight">fact-helper</span> n acc)
(<span class="keyword">if</span> (<= n 1)
acc
(fact-helper (- n 1) (* n acc))))
(fact-helper n 1))
</div>
</div>
<div class="section">
<h2>Conditional Expressions</h2>
<div class="code">
<span class="comment">;; Simple if</span>
(<span class="keyword">if</span> condition true-expr false-expr)
<span class="comment">;; Multiple conditions with cond</span>
(<span class="keyword">cond</span>
[condition1 expression1]
[condition2 expression2]
[<span class="keyword">else</span> default-expression])
<span class="comment">;; Example: Absolute value</span>
(<span class="keyword">define</span> (<span class="highlight">my-abs</span> x)
(<span class="keyword">if</span> (< x 0)
(- x)
x))
</div>
</div>
<div class="section">
<h2>Working Without Multiplication</h2>
<div class="code">
<span class="comment">;; Square using only addition</span>
(<span class="keyword">define</span> (<span class="highlight">square</span> n)
(<span class="keyword">define</span> (<span class="highlight">square-helper</span> n count acc)
(<span class="keyword">if</span> (= count (<span class="keyword">abs</span> n))
acc
(square-helper n (+ count 1) (+ acc (+ (<span class="keyword">abs</span> n) (<span class="keyword">abs</span> n) -1)))))
(<span class="keyword">if</span> (= n 0)
0
(square-helper n 0 0)))
<span class="comment">;; Using the pattern: n² = 1+3+5+...+(2n-1)</span>
</div>
</div>
<div class="section two-column">
<h2>Functional Programming Principles</h2>
<div>
<h3>Core Concepts</h3>
<ul>
<li><strong>Immutability:</strong> Values don't change</li>
<li><strong>Pure Functions:</strong> No side effects</li>
<li><strong>First-Class Functions:</strong> Functions as values</li>
<li><strong>Recursion:</strong> Instead of loops</li>
<li><strong>Expression-Based:</strong> Everything returns a value</li>
</ul>
<h3>Benefits</h3>
<ul>
<li>Easier to reason about</li>
<li>Natural parallelization</li>
<li>Fewer bugs</li>
<li>Mathematical elegance</li>
<li>Composability</li>
</ul>
</div>
</div>
<div class="section two-column">
<h2>Glossary of Terms</h2>
<table class="table">
<tr><th>Term</th><th>Definition</th></tr>
<tr><td><strong>Atom</strong></td><td>Indivisible data element (number, symbol, etc.)</td></tr>
<tr><td><strong>Expression</strong></td><td>Any valid Scheme code that evaluates to a value</td></tr>
<tr><td><strong>Form</strong></td><td>Syntactic structure in Scheme</td></tr>
<tr><td><strong>Lambda</strong></td><td>Anonymous function</td></tr>
<tr><td><strong>Predicate</strong></td><td>Function that returns true/false</td></tr>
<tr><td><strong>Procedure</strong></td><td>Named function</td></tr>
<tr><td><strong>S-Expression</strong></td><td>Symbolic expression (atoms or lists)</td></tr>
<tr><td><strong>Tail Recursion</strong></td><td>Recursive call is the last operation</td></tr>
<tr><td><strong>Higher-Order Function</strong></td><td>Function that takes/returns functions</td></tr>
<tr><td><strong>Closure</strong></td><td>Function with access to outer scope</td></tr>
<tr><td><strong>Base Case</strong></td><td>Stopping condition in recursion</td></tr>
<tr><td><strong>Recursive Case</strong></td><td>Case that calls itself</td></tr>
<tr><td><strong>Arity</strong></td><td>Number of arguments a function takes</td></tr>
<tr><td><strong>Side Effect</strong></td><td>Observable change outside function</td></tr>
<tr><td><strong>Referential Transparency</strong></td><td>Expression can be replaced by its value</td></tr>
</table>
</div>
<div class="section">
<h2>DrRacket Tips</h2>
<ul>
<li><strong>Language:</strong> Select "Determine language from source"</li>
<li><strong>Debugging:</strong> Use stepper for recursive visualization</li>
<li><strong>Testing:</strong> Put test cases at bottom of file</li>
<li><strong>Comments:</strong> Use ;; for line comments</li>
<li><strong>Indentation:</strong> DrRacket auto-indents properly</li>
<li><strong>Parentheses:</strong> Use Ctrl+] to match brackets</li>
</ul>
</div>
<div class="section">
<h2>Common Mistakes to Avoid</h2>
<div class="warning">
<ul>
<li>Missing parentheses in function calls</li>
<li>Using assignment instead of functional approach</li>
<li>Forgetting base cases in recursion</li>
<li>Mixing infix and prefix notation</li>
<li>Using display/write in pure functions</li>
<li>Not handling edge cases (negative numbers, zero)</li>
</ul>
</div>
</div>
</div>
</body>
</html>