Certified Racket AI Specialist ยท Building Intelligence
Hello again, young programmer! ๐ In Module One, we learned how to talk to the computer using Racket. We used numbers, words, and even made our own commands. But a computer that only adds numbers is like a robot that can only wave โ useful, but not very smart.
In this module, we will teach our computer to think and make decisions. We will learn about logic โ the rules of true and false. We will also learn how to compare things, make choices, and repeat actions. By the end, your programs will be able to decide what to do based on information, just like a real AI!
After this module, you will be able to:
Imagine you find a magic lamp. When you rub it, a genie appears. But this genie is very strict โ it only follows rules. You tell the genie: "If I say the magic word, give me a gift. Otherwise, do nothing." The genie checks: did you say the word? If yes, it gives you a gift. If no, it stays still.
That is exactly what an if statement does in Racket. It checks a condition. If the condition is true, it does one thing. If false, it does something else (or nothing).
In this module, we will give our programs the power to make decisions like that genie. We'll also learn how to combine decisions โ like: "If it's raining AND I have an umbrella, go out. Otherwise, stay inside."
+-------------------+
| Condition |
| (is it true?) |
+--------+----------+
|
+------+------+
| |
YES NO
| |
V V
Do action A Do action B
Definition: In Racket, true and false are values that tell us if something is correct or not. We write #t for true and #f for false.
Why important: Every decision a computer makes is based on true or false. If it's true, do one thing. If false, do another.
Simple explanation: Think of a light switch. On = true, Off = false. The computer can only be in one of these two states for any question.
Real-life example: "Is the sky blue?" โ yes (true). "Is water dry?" โ no (false).
School example: "Is 2 + 2 equal to 4?" โ true. "Is 5 less than 3?" โ false.
Home example: "Is it time for dinner?" โ maybe true or false depending on the clock.
Nigerian example: "Is Lagos a city in Nigeria?" โ true. "Is Abuja in Ghana?" โ false.
Illustration:
#t โ True (yes, correct) #f โ False (no, not correct) ( = 5 5 ) โ #t ( = 5 7 ) โ #f
Definition: We compare numbers using operators: > (greater), < (less), = (equal), >= (greater or equal), <= (less or equal).
Why important: AI uses comparisons all the time โ e.g., if a player's score is higher than the high score, update it.
Simple explanation: These are like the maths signs you know: > means bigger than, < means smaller than, = means same as.
Real-life: "Your age is greater than 10?" โ check if you are older than 10.
School: "Is 80 greater than 70?" โ yes, so you get a better grade.
Home: "Do we have more than 5 apples?" โ check the count.
Nigerian: "Is the price of groundnut oil less than 2000 Naira?" โ check to buy.
Illustration:
(> 10 5) โ #t (10 is greater than 5) (< 4 9) โ #t (4 is less than 9) (= 7 7) โ #t (7 is equal to 7) (>= 8 8) โ #t (8 is greater or equal to 8)
Definition: To compare words (strings), we use equal? or string=? to check if they are exactly the same.
Why important: AI often needs to check if a user said a certain word, e.g., "yes" or "no".
Simple explanation: It's like asking: "Is your name the same as my friend's name?"
Real-life: If you type "hello", the computer checks if that's equal to "hello".
School: "Is the answer 'B'?" โ check the string.
Home: "Did you say 'please'?" โ check the word.
Nigerian: "Did you say 'Abuja'?" โ check the city name.
Illustration:
(equal? "hello" "hello") โ #t (equal? "hello" "goodbye")โ #f (string=? "Ade" "Ade") โ #t
Definition: An if statement checks a condition. If the condition is true, it does the first thing; if false, it does the second thing.
Why important: This is the most common way to make decisions in programs.
Simple explanation: If it's raining, take an umbrella; otherwise, don't.
Real-life: If you are hungry, eat. Otherwise, play.
School: If you have homework, do it; otherwise, watch TV.
Home: If the light is on, turn it off; else, leave it.
Nigerian: If you have money, buy meat pie; otherwise, buy puff-puff.
Illustration:
(if ( > 5 3 ) ; condition: 5 > 3 ?
"yes, it is" ; true branch
"no, it isn't") ; false branch
โ "yes, it is"
Definition: cond is like an if statement but with many choices. It checks each condition one by one, and runs the first that is true.
Why important: Sometimes we have more than two options โ like choosing a meal from a menu.
Simple explanation: It's like a school grading system: if score โฅ 90 โ A, else if โฅ 80 โ B, else โ C.
Real-life: Traffic light: red โ stop, yellow โ wait, green โ go.
School: If you get 100, you get a star; if 80, a check; else, try again.
Home: If day is Monday, eat rice; Tuesday, eat yam; otherwise, eat beans.
Nigerian: If you are in Lagos, take the bus; if in Abuja, take a taxi; else, walk.
Illustration:
(cond
[(> 90 100) "A"] ; if false, skip
[(> 75 100) "B"] ; false
[else "C"]) ; else runs if nothing else true
โ "C"
Definition: We can combine true/false values using and, or, not. and is true only if all are true; or is true if at least one is true; not flips true to false and false to true.
Why important: Real decisions often need multiple conditions: "If it's sunny AND I'm free, I'll play outside."
Simple explanation: AND: both must be true. OR: at least one true. NOT: the opposite.
Real-life: You can watch TV if you finish homework AND your mom says yes.
School: You get a prize if you score 100 OR you are the best student.
Home: You can eat if the food is ready AND you set the table.
Nigerian: You can go to the market if you have money AND it's not raining.
Illustration:
(and #t #t) โ #t (and #t #f) โ #f (or #t #f) โ #t (or #f #f) โ #f (not #t) โ #f (not #f) โ #t
Let's build a calculator that can add, subtract, multiply, or divide based on the operator you choose. We'll use cond to pick the operation.
(define (calculate op a b)
(cond
[(equal? op "add") (+ a b)]
[(equal? op "sub") (- a b)]
[(equal? op "mul") (* a b)]
[(equal? op "div") (/ a b)]
[else "unknown operation"]))
(calculate "add" 5 3) โ 8
(calculate "mul" 4 2) โ 8
Definition: In cond, we can use else as the last option to catch anything that wasn't caught earlier.
Why important: It's like a safety net โ if nothing matches, do this.
Simple explanation: "If you have A, good; if B, okay; else, try again."
Real-life: If you have a ticket, enter; else, buy one.
School: If you answer all questions, you pass; else, you need to study more.
Home: If you finish your vegetables, you get dessert; else, no dessert.
Nigerian: If the bus arrives, board; else, wait.
Illustration:
(cond
[(= 5 6) "not"]
[else "everything else"])
โ "everything else"
Definition: You can put an if or cond inside another one. This is called nesting.
Why important: Some decisions depend on other decisions, like: "If you have money, then if the shop is open, buy something."
Simple explanation: It's like a game where you unlock a door, and behind that door is another door.
Real-life: If you have a phone, then if it has battery, you can call.
School: If you pass maths, then if you pass science, you get a prize.
Home: If it's your birthday, then if we have cake, we eat cake.
Nigerian: If you have transport money, then if the bus is available, go to the market.
Illustration:
(if (> age 12)
(if (equal? name "Ade")
"you are Ade and older"
"you are older but not Ade")
"you are young")
A decision tree is a picture of all the possible choices and their outcomes. It helps us plan our program.
Example: A chatbot that greets people based on the time of day.
Start
|
V
Ask: what is the hour?
|
V
+---------+---------+---------+
| | | |
Morning Afternoon Evening Night
| | | |
V V V V
"Good "Good "Good "Good
morning" afternoon" evening" night"
Definition: A function that returns #t or #f is called a boolean function. We use them to check conditions.
Why important: They help us build reusable tests.
Simple explanation: It's like a yes/no question that we can ask many times.
Real-life: A function that checks if a number is even: (even? 4) โ #t.
School: A function that checks if a student passed: (pass? score) โ #t if score โฅ 50.
Home: A function that checks if the door is locked.
Nigerian: A function that checks if someone is over 18 to vote.
Illustration:
(define (even? n) (= 0 (remainder n 2))) (even? 4) โ #t (even? 5) โ #f
We now build a program that asks for your age and tells you if you can drive, vote, or just play.
(define (age-check age)
(cond
[(< age 12) "You are too young to drive or vote. Play games!"]
[(< age 18) "You can't vote yet, but maybe you can learn to drive."]
[(< age 60) "You can drive and vote!"]
[else "You are a wise elder. Rest and enjoy."]))
(age-check 10) โ "You are too young..."
(age-check 20) โ "You can drive and vote!"
This is a real AI-like program that gives different answers based on your age. You are now building intelligence!
How to write a decision program:
Emphasize that logic is everywhere. Use physical examples: "If I clap, you sit down. Else, stand up." Use cards with #t and #f. Let students write small programs with if and cond. Encourage them to test different inputs.
Ask your child to explain the difference between AND and OR. Play the "if game" at home: "If you clean your room, then you can have a snack." This builds logical thinking. Celebrate when they get the logic right!
Racket's cond is so flexible that you can write entire programs using only cond and recursion. Many AI systems are built using these same ideas!
= to compare strings โ use equal?.and and or are operators, not keywords.else in cond โ your program might return nothing.cond for more than 3 options.
Decision Flow:
+---------+ +--------+ +--------+
| Condition | --> | #t? | --> | Action1 |
+---------+ +--------+ +--------+
|
V
+--------+ +--------+
| #f? | --> | Action2 |
+--------+ +--------+
AND logic:
Input1 ---+
AND ---> Output
Input2 ---+
| Operator | Meaning | Example | Result |
|---|---|---|---|
| > | greater than | (> 10 5) | #t |
| < | less than | (< 2 5) | #t |
| = | equal (numbers) | (= 3 3) | #t |
| equal? | equal (any type) | (equal? "hi" "hi") | #t |
| and | both true | (and #t #t) | #t |
| or | at least one true | (or #f #t) | #t |
| not | flips | (not #t) | #f |
Amazing! You've completed Module Two. You now know how to make your programs decide things. You learned about true/false, comparison operators, if, cond, and logic combiners. You even built a smart age advisor. You are thinking like a real programmer. These decision-making skills are the heart of artificial intelligence. Every AI system, from self-driving cars to smart assistants, uses these same logical building blocks.
| Term | Match |
|---|---|
| 1. #t | A. false |
| 2. #f | B. true |
| 3. cond | C. many choices |
| 4. if | D. two choices |
Answers: 1-B, 2-A, 3-C, 4-D
Scenario: You are building a smart trash can that opens when a person approaches, but only if it's not raining. How would you write the logic?
Answer: (if (and person-approaches (not raining)) "open" "stay closed")
Each group writes a "fortune teller" program. It asks for your age and gives a fortune: if under 10, "you will be a pilot"; if 10-15, "you will be a scientist"; else, "you will be a leader". Use cond.
Write a function called can-vote? that takes age and returns #t if age โฅ 18, else #f.
Create a "traffic light" program. Use cond with a color input: "red" โ stop, "yellow" โ wait, "green" โ go, else โ "invalid".
Write a program that takes a student's score and outputs: "Excellent" for 90+, "Good" for 70-89, "OK" for 50-69, and "Needs Improvement" for below 50.
Write a function that takes three numbers and returns the largest. Use cond and comparisons.
Multiple choice answers: 1-A, 2-A, 3-A, 4-B, 5-B, 6-A, 7-B, 8-B, 9-A, 10-A, 11-A, 12-A, 13-A, 14-D, 15-A.
In Module Three, we will learn about repetition โ how to make the computer repeat actions using loops and recursion. We'll build programs that can handle lots of data and do tasks over and over. Practice your logic skills, and get ready to make your programs even more powerful!
๐ You've completed Module Two ยท Keep up the great work, young AI specialist! ๐