Certified Racket AI Specialist ยท Building Smart Containers
Hello, young organiser! ๐ In Module Three, we learned to repeat tasks and process lists. But real-world data is often more complex than a simple list. We need better ways to organise information.
Think about a school. You don't just have a list of students โ you also have their names, ages, classes, and scores. You need to keep all that related information together. That's where data structures come in.
In this module, we will learn about structures (custom containers for related data), association lists (key-value pairs), and trees (hierarchical data). These tools will help you build smarter, more realistic AI systems.
After this module, you will be able to:
struct.Imagine you are in charge of a huge library. You have thousands of books. If you just pile them up, nobody can find anything. But if you organise them by category, author, and title, everything becomes easy to find.
In programming, data structures are like your library organisation system. They help you store, find, and use information quickly and correctly.
Today, we will build our own "organisers" โ structures to hold related information, dictionaries to look things up, and trees to show relationships. Let's get organised!
Without structure:
[book1, book2, book3, ...] (confusing)
With structure:
[ (title "Racket" author "Smith" year 2020),
(title "AI" author "Jones" year 2021) ]
(clear and organised)
Definition: A data structure is a way to store and organise data so that we can use it easily.
Why important: AI systems handle lots of data. Good structures make AI faster and easier to build.
Simple explanation: Think of a drawer. If you throw everything in, it's messy. If you use dividers, you can find things quickly. Data structures are like dividers.
Real-life: A phone book organises names and numbers.
School: A timetable organises subjects by day and time.
Home: A recipe book organises dishes by type (soup, rice, etc.).
Nigerian: A market seller organises goods by type: yams here, tomatoes there.
Illustration:
Without structure: [2, "Ade", #t, 5.6, "hello"] With structure: (person name "Ade" age 12)
Definition: A structure (struct) is a custom data type that groups related pieces of information together.
Why important: Structures let you create your own data types, like "Student" or "Book", with named fields.
Simple explanation: It's like a form you fill out: name, age, class. Each person gets their own form.
Real-life: A passport contains name, photo, nationality.
School: A report card contains subject, score, grade.
Home: A gift card contains sender, receiver, amount.
Nigerian: A voter's card contains name, age, polling unit.
Illustration:
(struct student (name age class)) (define s1 (student "Ade" 12 "JSS2")) (student-name s1) โ "Ade" (student-age s1) โ 12
Let's create a structure for a book: title, author, pages.
(struct book (title author pages)) (define b1 (book "Racket for AI" "Dr. Ada" 120)) (book-title b1) โ "Racket for AI" (book-author b1) โ "Dr. Ada" (book-pages b1) โ 120
You can also make lists of structures.
Definition: An association list (alist) is a list of pairs: (key . value). It acts like a dictionary or lookup table.
Why important: AI often needs to look up values by key โ like finding a word in a dictionary.
Simple explanation: It's like a label on a box: the label (key) tells you what's inside (value).
Real-life: A dictionary: word โ meaning.
School: A class list: name โ score.
Home: A chore chart: chore โ who does it.
Nigerian: A menu: food โ price.
Illustration:
(define phone-list '((Ade . 080123) (Bola . 080456))) (assoc 'Ade phone-list) โ '(Ade . 080123) (cdr (assoc 'Ade phone-list)) โ 080123
We can add, find, and update values in alists. Racket provides functions like assoc and alist-update.
Definition: A tree is a data structure that has a root and branches. Each branch can have more branches. It's used for hierarchical data.
Why important: Many things are hierarchical: family trees, file systems, decision trees in AI.
Simple explanation: It's like a family tree: grandparents at the top, parents below, children below them.
Real-life: A company organisation chart: CEO โ managers โ employees.
School: A school system: Principal โ Head teachers โ Class teachers โ Students.
Home: A wardrobe: clothes โ tops โ shirts.
Nigerian: Traditional title system: Oba โ chiefs โ villagers.
Illustration:
Root
/ \
NodeA NodeB
/ \ |
Leaf1 Leaf2 Leaf3
We can represent a tree using lists: (value children...). For example: (1 (2 (4) (5)) (3 (6)))
(define tree '(1 (2 (4) (5)) (3 (6)))) ; tree with root 1, children 2 and 3, etc.
We can write functions to traverse trees.
Better: use structures to make trees clearer.
(struct node (value left right)) (define tree (node 5 (node 3 #f #f) (node 7 #f #f)))
This is a binary tree: each node has a value, left child, and right child.
We can visit every node in a tree (called traversal). There are different orders: pre-order, in-order, post-order.
(define (in-order tree)
(if (empty? tree)
'()
(append (in-order (node-left tree))
(list (node-value tree))
(in-order (node-right tree)))))
| Structure | Use when | Example |
|---|---|---|
| List | Simple ordered data | scores of a game |
| Struct | Related fields | student record |
| Association list | Lookup by key | dictionary |
| Tree | Hierarchical data | family tree |
Let's build a system that manages books using structures and association lists.
(struct book (title author year))
(define library '())
(define (add-book library b) (cons b library))
(define (find-by-author library author)
(filter (lambda (b) (equal? (book-author b) author)) library))
This is a simple AI for managing a library!
Create a student structure with name, age, and scores. Store them in a list. Write functions to find students by name and to calculate average score.
How to create a structure and use it:
(struct person (name age))(define p (person "Ada" 12))(person-name p) โ "Ada"(list (person "Bob" 10) (person "Eve" 11))Use physical objects: folders for structs, sticky notes for alists, family trees for trees. Encourage students to think about organising their own belongings. Relate every structure to something tangible.
Ask your child to describe how they organise their room. Relate that to data structures. Encourage them to think about how a library or shop organises items. Practice creating structures for things at home.
Racket's struct can automatically generate functions for you: a constructor, field accessors, and even a predicate (like student?).
struct before creating an instance.assoc on an alist with improper formatting.student, book, etc.empty? to check for empty lists and trees.Structure example: +------------------------+ | Student | +------------------------+ | name: "Ade" | | age: 12 | | class: "JSS2" | +------------------------+
Tree example:
+-----+
| 10 | (root)
+-----+
/ \
+---+ +---+
| 5 | | 15 |
+---+ +---+
/ \ \
+---+ +---+ +---+
| 2 | | 7 | | 20 |
+---+ +---+ +---+
| Structure | Use | Access |
|---|---|---|
| List | ordered items | first, rest |
| Struct | named fields | field functions |
| Alist | lookup | assoc |
| Tree | hierarchy | recursive traversal |
You've learned how to organise data like a pro! You can now create structures, use association lists, and build trees. These tools are essential for any AI system โ they help you store, find, and manage information efficiently. You're building the foundation for intelligent data handling.
| Term | Match |
|---|---|
| 1. struct | A. key-value pairs |
| 2. alist | B. hierarchical data |
| 3. tree | C. custom fields |
Answers: 1-C, 2-A, 3-B
Scenario: You are building a school management system. You need to store student details (name, age, class) and also a list of their subjects and scores. How would you organise this using data structures?
Answer: Use a struct for student, and inside it, use an alist or another struct for subjects and scores.
Each group designs a data structure for a "shop inventory" โ products with name, price, and quantity. They also create an alist for discounts. Present the design.
Write a struct for a "phone" with brand, model, and price. Create a list of three phones and write a function to find the most expensive phone.
Build a "library manager" that stores books (title, author, year) and allows you to add, list, and find books by author.
Create a student database using structs. Write functions to add a student, find a student by name, and calculate the average age of all students.
Write a function that takes a binary tree and returns the sum of all values. (Assume each node has a value and left/right children.)
Multiple choice answers: 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 Five, we will dive into recursion on trees and more advanced data processing. You'll learn to manipulate trees, build decision trees for AI, and combine structures with recursion. Keep practising with structures and lists โ they will be your main tools!
๐ You've completed Module Four ยท You are now a data structure master! ๐