Scheme & Functional Programming Cheat Sheet
Basic Syntax & Notation
Prefix Notation
(+ 1 2 3 4)
(* 2 3 4)
(- 10 3)
(/ 20 4)
Basic Forms
42, 3.14, -17, 1/3
#t, #f
#\a, #\b, #\space, #\newline
"Hello World"
'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
(define (procedure-name param1 param2)
body-expression)
(define (add x y)
(+ x y))
(add 5 3)
(lambda (x y) (+ x y))
((lambda (x y) (+ x y)) 5 3)
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)
(if (<= n 1)
1
(* n (factorial (- n 1)))))
(factorial 5)
(factorial 0)
Fibonacci Example
(define (fib n)
(cond
[(<= n 0) 0]
[(= n 1) 1]
[else (+ (fib (- n 1)) (fib (- n 2)))]))
(fib 0)
(fib 9)
Data Types & Predicates
| Type | Predicate | Example |
| Number | number? | (number? 42) → #t |
| String | string? | (string? "hi") → #t |
| Symbol | symbol? | (symbol? 'x) → #t |
| Character | char? | (char? #\a) → #t |
| Boolean | boolean? | (boolean? #t) → #t |
| List | list? | (list? '(1 2 3)) → #t |
| Null | null? | (null? '()) → #t |
Input/Output
(read)
(define (read-for-square)
(let ([input (read)])
(* input input)))
(* (read) (read))
Note: Avoid display, write, and begin forms in pure functional code
Common Recursive Patterns
Linear Recursion
(define (sum-odds max)
(cond
[(<= max 0) 0]
[(odd? max) (+ max (sum-odds (- max 2)))]
[else (sum-odds (- max 1))]))
Tree Recursion
Tail Recursion (with helpers)
(define (factorial-tail n)
(define (fact-helper n acc)
(if (<= n 1)
acc
(fact-helper (- n 1) (* n acc))))
(fact-helper n 1))
Conditional Expressions
(if condition true-expr false-expr)
(cond
[condition1 expression1]
[condition2 expression2]
[else default-expression])
(define (my-abs x)
(if (< x 0)
(- x)
x))
Working Without Multiplication
(define (square n)
(define (square-helper n count acc)
(if (= count (abs n))
acc
(square-helper n (+ count 1) (+ acc (+ (abs n) (abs n) -1)))))
(if (= n 0)
0
(square-helper n 0 0)))
Functional Programming Principles
Core Concepts
- Immutability: Values don't change
- Pure Functions: No side effects
- First-Class Functions: Functions as values
- Recursion: Instead of loops
- Expression-Based: Everything returns a value
Benefits
- Easier to reason about
- Natural parallelization
- Fewer bugs
- Mathematical elegance
- Composability
Glossary of Terms
| Term | Definition |
| Atom | Indivisible data element (number, symbol, etc.) |
| Expression | Any valid Scheme code that evaluates to a value |
| Form | Syntactic structure in Scheme |
| Lambda | Anonymous function |
| Predicate | Function that returns true/false |
| Procedure | Named function |
| S-Expression | Symbolic expression (atoms or lists) |
| Tail Recursion | Recursive call is the last operation |
| Higher-Order Function | Function that takes/returns functions |
| Closure | Function with access to outer scope |
| Base Case | Stopping condition in recursion |
| Recursive Case | Case that calls itself |
| Arity | Number of arguments a function takes |
| Side Effect | Observable change outside function |
| Referential Transparency | Expression can be replaced by its value |
DrRacket Tips
- Language: Select "Determine language from source"
- Debugging: Use stepper for recursive visualization
- Testing: Put test cases at bottom of file
- Comments: Use ;; for line comments
- Indentation: DrRacket auto-indents properly
- Parentheses: Use Ctrl+] to match brackets
Common Mistakes to Avoid
- Missing parentheses in function calls
- Using assignment instead of functional approach
- Forgetting base cases in recursion
- Mixing infix and prefix notation
- Using display/write in pure functions
- Not handling edge cases (negative numbers, zero)