Certified Racket AI Specialist ยท Power of Doing Things Again and Again
Hello, clever coder! ๐ In Module Two, we taught our computer to make decisions. Now we will teach it something even more powerful: how to repeat tasks.
Think about a mother hen counting her chicks. She doesn't just count once โ she counts each chick, one by one. If she had to count 100 chicks, she would do the same action 100 times. Computers are great at this! They never get bored or tired.
In this module, we will learn loops (ways to repeat code) and recursion (a function that calls itself). We'll build programs that can handle big lists, draw patterns, and solve problems that need many steps. By the end, you'll be able to make your computer do the hard work for you โ just like a real AI!
After this module, you will be able to:
Imagine you are at a party, and the DJ plays a song. Everyone starts dancing. When the song ends, the DJ plays the same song again โ and again โ and again. People keep dancing until the DJ stops.
That's like a loop in programming. The computer does the same action over and over until we tell it to stop.
But there is another way: imagine a line of people. You ask the first person, "What's your name?" They tell you. Then you ask the second person, and so on, until you reach the end of the line. In Racket, we use recursion to do this โ a function that keeps calling itself until it reaches the end.
Let's learn both ways, starting with recursion โ the Racket way!
Loop (repeat)
+--------+
| Do work |
+--------+
|
V
+--------+
| Repeat? |
+--------+
|
Yes/No
Definition: Repetition means doing the same thing more than once.
Why important: Many tasks in AI involve doing the same thing to a lot of data โ like checking all the numbers in a list, or training a model by repeating steps.
Simple explanation: If you want to eat 10 candies, you pick one, eat it, pick one, eat it... you repeat the "pick and eat" action 10 times.
Real-life example: A teacher checking homework for 30 students โ checks each student's work one by one.
School example: Writing your name 5 times to practice.
Home example: Taking out all the dishes from a dishwasher โ one plate at a time.
Nigerian example: Counting the number of mangoes in a basket โ pick one, count, pick one, count.
Illustration:
Task: Count apples Step 1: take one apple, count 1 Step 2: take one apple, count 2 Step 3: take one apple, count 3 ... repeat until no apples left
Definition: Recursion is when a function calls itself to solve a smaller version of the same problem.
Why important: Recursion is the main way to repeat things in Racket. It's elegant and powerful.
Simple explanation: Imagine you are in a maze. You look around. If you see the exit, you go out. If not, you take one step and then solve the maze again. That's recursion โ solving a smaller version of the same problem.
Real-life: Looking for a book in a stack: you check the top, if it's not there, you check the next, and so on.
School: Counting down to zero: "5, 4, 3, 2, 1, blast off!" Each step is a smaller count.
Home: Unwrapping a gift: you open one layer, if there's another layer, you open that too.
Nigerian: Sharing oranges: give one to a friend, then share the rest.
Illustration:
(define (count-down n)
(if (<= n 0)
"blast off!"
(begin
(display n)
(count-down (- n 1)))))
(count-down 5)
โ 5 4 3 2 1 blast off!
Definition: The base case is the condition that stops the recursion. Without it, the function would run forever.
Why important: Every recursive function must have a base case to avoid infinite loops.
Simple explanation: It's like a "STOP" sign. When you reach the base case, you don't continue.
Real-life: In a race, the finish line is the base case โ when you cross it, you stop.
School: When you finish a test, you stop writing.
Home: When the pot is empty, you stop serving food.
Nigerian: When the market closes, you stop buying.
Illustration:
(define (sum-up-to n)
(if (= n 0) ; base case
0 ; stop
(+ n (sum-up-to (- n 1)))))
(sum-up-to 5) โ 15 (5+4+3+2+1+0)
Definition: We can use recursion to go through each item in a list and do something with it.
Why important: AI often works with lists of data โ like scores, names, or sensor readings.
Simple explanation: Imagine a line of students. You ask the first student a question, then move to the next, until you reach the end.
Real-life: Checking a shopping list: look at first item, buy it, then go to the next.
School: Taking attendance: call each name in the list.
Home: Reading a list of chores: do chore 1, then chore 2, etc.
Nigerian: Going through a list of market items: buy yam, then pepper, then onions.
Illustration:
(define (sum-list lst)
(if (empty? lst) ; base case: empty list
0
(+ (first lst) (sum-list (rest lst)))))
(sum-list '(1 2 3 4)) โ 10
Definition: map takes a function and a list, and applies that function to every item in the list, returning a new list.
Why important: It's a fast way to transform all items without writing a loop.
Simple explanation: It's like a teacher giving a sticker to every student in the class โ one function applied to everyone.
Real-life: Adding tax to every item in a shopping cart.
School: Adding 5 points to everyone's test score.
Home: Cutting every fruit into pieces.
Nigerian: Adding 10% discount to every item in a price list.
Illustration:
(define (double x) (* x 2)) (map double '(1 2 3 4)) โ '(2 4 6 8)
Definition: filter takes a test function and a list, and returns a new list with only the items that pass the test.
Why important: AI often needs to pick out certain data โ like finding all numbers greater than 50.
Simple explanation: It's like a sieve โ you shake it and only small particles pass through. Filter keeps only the items that are true.
Real-life: Sorting out ripe fruits from unripe ones.
School: Picking all students who scored above 70.
Home: Selecting only your favorite toys.
Nigerian: Choosing only the big fish from a basket.
Illustration:
(define (even? n) (= 0 (remainder n 2))) (filter even? '(1 2 3 4 5)) โ '(2 4)
Definition: foldl (or fold) takes a function, an initial value, and a list, and combines all items into one value.
Why important: It's used for summing, multiplying, or combining data in many AI tasks.
Simple explanation: Think of a big pile of numbers. You start with 0, then add each number one by one to get the total.
Real-life: Adding up all the prices in a shopping cart.
School: Calculating the total marks for all subjects.
Home: Counting how many toys you have by adding 1 each time.
Nigerian: Summing the cost of all items in a market list.
Illustration:
(foldl + 0 '(1 2 3 4)) โ 10 (foldl * 1 '(1 2 3 4)) โ 24
Let's build a function that counts the number of items in a list (without using length).
(define (my-length lst)
(if (empty? lst)
0
(+ 1 (my-length (rest lst)))))
(my-length '(a b c d)) โ 4
Steps: 1) If list is empty, return 0. 2) Otherwise, count 1 for the first item, and add the length of the rest.
Factorial is a famous recursion example. 5! = 5 ร 4 ร 3 ร 2 ร 1.
(define (factorial n)
(if (= n 0)
1
(* n (factorial (- n 1)))))
(factorial 5) โ 120
AI uses factorial in probability and statistics.
In Racket, we use recursion instead of traditional loops (like 'for' or 'while' in other languages). Recursion is more functional and elegant.
| Language | Loop style |
|---|---|
| Python | for i in range(5): print(i) |
| Racket | (define (loop n) (if (> n 0) (begin (display n) (loop (- n 1))) 'done)) |
AI systems process huge lists of data. We can use map, filter, and fold to clean and analyze data. For example, we can filter out low scores, map each score to a grade, and fold to get the average.
Let's build a program that takes a list of numbers, keeps only the even ones, doubles them, and sums them up.
(define (process nums)
(foldl + 0 (map double (filter even? nums))))
(process '(1 2 3 4 5 6)) โ 2*2 + 4*2 + 6*2 = 4+8+12 = 24
You just built a tiny data pipeline โ the same idea used in big AI systems!
How to write a recursive function:
Use physical demonstrations: pass a ball around a circle to show recursion. Use boxes to represent lists. Emphasize that recursion is just a function calling itself. Always write the base case first.
Encourage your child to think about patterns of repetition in daily life. Ask: "How would you tell a robot to count all your toys?" This builds the concept of iteration. Practice writing small recursive functions together.
Racket's map can work with multiple lists at the same time! For example: (map + '(1 2) '(3 4)) โ '(4 6).
rest to shrink the list.map and filter โ map returns same length, filter returns shorter.foldl with wrong initial value.
Recursive process:
function(5)
|
V
function(4)
|
V
function(3)
|
V
function(2)
|
V
function(1)
|
V
function(0) โ base case, returns
Map process: [1,2,3] -- map double --> [2,4,6]
| Function | What it does | Returns |
|---|---|---|
| map | transforms each item | list of same length |
| filter | keeps items that pass test | shorter list |
| foldl | combines all items | single value |
You've mastered repetition in Racket! You learned recursion, map, filter, and fold. You can now process lists of any size. These skills are essential for AI โ from cleaning data to training models. You are building a powerful toolkit.
| Term | Match |
|---|---|
| 1. map | A. combines items |
| 2. filter | B. transforms each |
| 3. fold | C. keeps some |
| 4. recursion | D. calls itself |
Answers: 1-B, 2-C, 3-A, 4-D
Scenario: You have a list of temperatures in Celsius. Write a program that converts them to Fahrenheit (C ร 9/5 + 32) and then finds the average.
Answer: (define (avg lst) (/ (foldl + 0 lst) (length lst))) (avg (map (lambda (c) (+ (* c 1.8) 32)) temps))
Each group writes a function that takes a list of words and returns a list of their lengths. Use map.
Write a recursive function that returns the last element of a list.
Create a "smart filter" for a list of numbers: keep only the numbers that are perfect squares (like 1,4,9,16). Use filter and a helper function.
Write a program that takes a list of exam scores, removes scores below 40, gives a 5-point bonus to the rest, and finds the total.
Write a recursive function that reverses a list. (Hint: append the first item to the reverse of the rest.)
Multiple choice: 1-A, 2-A, 3-A, 4-A, 5-A, 6-A, 7-A, 8-A, 9-A, 10-A, 11-A, 12-A, 13-A, 14-A, 15-A.
In Module Four, we will dive into data structures โ more ways to organize information. We'll learn about structures, association lists, and trees. You'll be able to build more complex AI systems. Practice your recursion and list functions โ they will be your best friends!
๐ You've completed Module Three ยท You are now a Racket repetition expert! ๐