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

ProcedureDescriptionExample
defineCreate variables/procedures(define x 5)
ifConditional expression(if (> x 0) "pos" "neg")
condMultiple conditions(cond [(> x 0) "pos"] [else "neg"])
andLogical AND(and #t #f) → #f
orLogical OR(or #t #f) → #t
notLogical NOT(not #t) → #f
absAbsolute 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:
  1. Size-n Problem: What are we trying to solve?
  2. Stopping Conditions: When do we stop recursing?
  3. Size-m Problems: What smaller problems do we solve?
  4. Construction: How do we build the solution from smaller parts?

Factorial Example

(define (factorial n) ;; 1. Size-n problem: factorial of n (if (<= n 1) ;; 2. Stopping condition: n <= 1, return 1 1 ;; 3. Size-m problem: factorial of (n-1) ;; 4. Construction: n * factorial(n-1) (* n (factorial (- n 1))))) ;; Test cases (factorial 5) ;; → 120 (factorial 0) ;; → 1

Fibonacci Example

(define (fib n) ;; 1. Size-n problem: nth Fibonacci number (cond ;; 2. Stopping conditions [(<= n 0) 0] [(= n 1) 1] ;; 3&4. Size-m problems and construction [else (+ (fib (- n 1)) (fib (- n 2)))])) ;; Test cases (fib 0) ;; → 0 (fib 9) ;; → 34

Data Types & Predicates

TypePredicateExample
Numbernumber?(number? 42) → #t
Stringstring?(string? "hi") → #t
Symbolsymbol?(symbol? 'x) → #t
Characterchar?(char? #\a) → #t
Booleanboolean?(boolean? #t) → #t
Listlist?(list? '(1 2 3)) → #t
Nullnull?(null? '()) → #t

Input/Output

;; Reading input (read) ;; Read from keyboard ;; Example: Read and square (define (read-for-square) (let ([input (read)]) (* input input))) ;; Multiple reads (* (read) (read)) ;; Multiply two inputs
Note: Avoid display, write, and begin forms in pure functional code

Common Recursive Patterns

Linear Recursion

;; Sum of odd numbers up to max (define (sum-odds max) (cond [(<= max 0) 0] [(odd? max) (+ max (sum-odds (- max 2)))] [else (sum-odds (- max 1))]))

Tree Recursion

;; Already shown with Fibonacci ;; Two recursive calls create tree structure

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

;; Simple if (if condition true-expr false-expr) ;; Multiple conditions with cond (cond [condition1 expression1] [condition2 expression2] [else default-expression]) ;; Example: Absolute value (define (my-abs x) (if (< x 0) (- x) x))

Working Without Multiplication

;; Square using only addition (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))) ;; Using the pattern: n² = 1+3+5+...+(2n-1)

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

TermDefinition
AtomIndivisible data element (number, symbol, etc.)
ExpressionAny valid Scheme code that evaluates to a value
FormSyntactic structure in Scheme
LambdaAnonymous function
PredicateFunction that returns true/false
ProcedureNamed function
S-ExpressionSymbolic expression (atoms or lists)
Tail RecursionRecursive call is the last operation
Higher-Order FunctionFunction that takes/returns functions
ClosureFunction with access to outer scope
Base CaseStopping condition in recursion
Recursive CaseCase that calls itself
ArityNumber of arguments a function takes
Side EffectObservable change outside function
Referential TransparencyExpression can be replaced by its value

DrRacket Tips

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)