Procedures & Lists - Complete Interactive Lesson
Part 1: Core Concepts
📝 Procedures & Lists
Part 1 of 7 — List Operations, Traversals, and Procedure Design
Lists in AP CSP
A list (also called an array) stores an ordered collection of values.
// AP CSP pseudocode uses 1-based indexing!
myList ← ["apple", "banana", "cherry"]
DISPLAY(myList[1]) // "apple" (index 1 is the first element)
DISPLAY(myList[3]) // "cherry"
⚠️ AP CSP lists are 1-indexed (first element at index 1). Most programming languages are 0-indexed.
List Operations
| Operation | Pseudocode | Effect |
|---|---|---|
| Access | list[i] | Get element at index i |
| Assign | list[i] ← value | Set element at index i |
| Insert | INSERT(list, i, value) | Insert value at index i, shifting others right |
| Append | APPEND(list, value) | Add value to the end of the list |
| Remove | REMOVE(list, i) | Remove element at index i, shifting others left |
| Length | LENGTH(list) | Number of elements in the list |
Example
scores ← [90, 85, 78]
APPEND(scores, 92) // [90, 85, 78, 92]
INSERT(scores, 2, 88) // [90, 88, 85, 78, 92]
REMOVE(scores, 1) // [88, 85, 78, 92]
DISPLAY(LENGTH(scores)) // 4
Concept Check 🎯
List Traversal
FOR EACH Loop
total ← 0
FOR EACH score IN scores
{
total ← total + score
}
average ← total / LENGTH(scores)
Index-Based Loop
i ← 1
REPEAT UNTIL (i > LENGTH(scores))
{
DISPLAY(scores[i])
i ← i + 1
}
Common List Algorithms
Find the Maximum
PROCEDURE findMax(list)
{
max ← list[1]
FOR EACH item IN list
{
IF (item > max)
{
max ← item
}
}
RETURN max
}
Filter a List
PROCEDURE getPositives(list)
{
result ← []
FOR EACH item IN list
{
IF (item > 0)
{
APPEND(result, item)
}
}
RETURN result
}
Swap Two Elements
temp ← list[i]
list[i] ← list[j]
list[j] ← temp
Applied Recall ✍️
-
APPEND(list, value) adds the value to the _______ of the list.
-
REMOVE(list, i) removes the element at index i and shifts remaining elements _______.
-
To swap two list elements without losing data, you need a _______ variable.
List Operations 🔍
AP Exam Strategy: Procedures & Lists
- Lists are 1-indexed on the AP exam — first element is list[1], NOT list[0]
- Know all list operations: access, assign, INSERT, APPEND, REMOVE, LENGTH
- INSERT shifts elements RIGHT (list grows). REMOVE shifts elements LEFT (list shrinks)
- The swap algorithm (using temp) appears frequently — memorize it
- Common patterns: sum/average, find max/min, filter, count matches
- FOR EACH is read-only for the loop variable — changes to the variable do NOT change the list
AP-Style Application 🎯
Part 2: Key Processes
📋 Procedures & Lists
Part 2 of 7 — Key Processes
Procedures Bundle Reusable Logic
A procedure (a.k.a. function or method) is a named, reusable block of instructions. Define once, call many times.
PROCEDURE greet(name) DISPLAY("Hello, " + name)
Calling greet("Alex") runs the body with name = "Alex".
| Concept | Meaning |
|---|---|
| Parameter | Variable in the procedure's definition. |
| Argument | Value passed in when calling. |
| Return value | What the procedure produces. |
| Call | The act of running the procedure. |
Concept Check 🎯
Why Procedures Matter
| Benefit | Explanation |
|---|---|
| Reuse | Write logic once, call from many places. |
| Abstraction | Caller doesn't need to know HOW it works. |
Part 3: Patterns & Examples
📋 Procedures & Lists
Part 3 of 7 — Patterns & Examples
Procedure Patterns
| Pattern | Use |
|---|---|
| Pure function | Inputs → output, no side effects. |
| Predicate | Returns true / false. |
| Constructor | Builds a structured value. |
| Action | Performs a side effect (print, save). |
| Higher-order | Takes another procedure as a parameter. |
List Patterns
| Pattern | Skeleton |
|---|---|
| Map | new = []; FOR EACH x IN list: APPEND(new, f(x)) |
| Filter | new = []; FOR EACH x IN list: IF p(x) THEN APPEND(new, x) |
| Reduce / fold | acc = init; FOR EACH x IN list: acc = combine(acc, x) |
Concept Check 🎯
Map / Filter / Reduce In Pseudocode
Part 4: Connections & Interactions
📋 Procedures & Lists
Part 4 of 7 — Connections & Interactions
Procedures & Lists Connect Across CSP
| Connection | Why |
|---|---|
| Procedures ↔ Algorithms | Algorithms are usually packaged as procedures. |
| Lists ↔ Data | Lists are the simplest structured data. |
| Procedures ↔ Collaboration | Reusable named procedures help teams divide work. |
| Procedures ↔ Testing | Procedures are testable units. |
Concept Check 🎯
Lists Are Often Procedure Inputs / Outputs
Most non-trivial procedures take or return lists:
PROCEDURE topThree(scores) sorted ← SORT(scores, descending) RETURN [sorted[1], sorted[2], sorted[3]]
Mutability Caution
When a procedure receives a list, modifying it may modify the caller's list (depending on language). To stay safe, document whether your procedure mutates inputs or returns a fresh list.
Pure Functions Are Easier To Test
A pure function (no side effects, output depends only on inputs) can be tested with simple input → expected-output pairs:
| Input | Expected output |
|---|---|
| [3, 1, 2] | 2.0 |
Part 5: Change Over Time
📋 Procedures & Lists
Part 5 of 7 — Change Over Time
How Procedures & Lists Have Evolved
| Era | Trend |
|---|---|
| 1970s | Procedural programming (subroutines). |
| 1990s | Object-oriented (methods on objects). |
| 2000s | Functional resurgence (map/filter/reduce in mainstream langs). |
| 2010s | Lambdas / arrow functions everywhere. |
| 2020s | Pattern matching, immutable collections by default in many new langs. |
Concept Check 🎯
Lists Got A Cousin: Other Collections
| Collection | Property |
|---|---|
| List / array | Ordered, indexed. |
| Set | Unordered, no duplicates. |
| Map / dictionary | Key → value. |
| Tuple | Fixed-size ordered group. |
| Stream | Lazy, possibly infinite sequence. |
AP CSP focuses on lists, but real software uses all of these.
Part 6: Problem-Solving Workshop
📋 Procedures & Lists
Part 6 of 7 — Problem-Solving Workshop
Procedures & Lists Workshop
Concept Check 🎯
Worked: Build A "Top K" Procedure
PROCEDURE topK(scores, k) sorted ← SORT(scores, descending) result ← [] FOR i FROM 1 TO k: APPEND(result, sorted[i]) RETURN result
Now any caller can ask for the top 3, top 10, etc.
Worked: Compose Map + Filter + Reduce
Sum the squares of even numbers in nums:
total ← 0 FOR EACH n IN nums: IF n MOD 2 = 0 THEN total ← total + n * n
Conceptually: filter (even) → map (square) → reduce (sum).
Worked: 2D Sum
PROCEDURE gridSum(grid) total ← 0 FOR EACH row IN grid: FOR EACH cell IN row: total ← total + cell RETURN total
Applied Recall ✍️
-
A "top K" procedure usually first _______ the list, then takes the first k elements.
-
Filter then map then reduce is a common functional _______.
-
Touching every cell in a 2D list requires _______ loops.
Targeted Practice 🔍
AP Exam Strategy: Workshop
- Recognize map / filter / reduce shape from the loop body.
- Top-K = sort + take.
- 2D traversal = nested loops.
AP-Style Application 🎯
Part 7: AP Review
📋 Procedures & Lists
Part 7 of 7 — AP Review
AP Exam Recap — Procedures & Lists
Concept Check 🎯
Final Vocab
| Term | Definition |
|---|---|
| Procedure | Named, reusable block of code. |
| Parameter | Variable in the definition. |
| Argument | Value at the call site. |
| Return | Value the procedure produces. |
| List | Ordered indexed collection. |
| LENGTH | Number of elements. |
| APPEND | Add to end. |
| Map / filter / reduce | Three core list patterns. |
| Higher-order procedure | Takes / returns procedures. |
| Pure function | No side effects. |
Common Pitfalls
- Confusing parameter and argument.
- Off-by-one with 1-indexed lists.
- Forgetting to RETURN a value.
- Mutating a caller's list without documenting it.
- Writing one big function instead of decomposing.
✍️