Computer Mathematics Program to Demonstrate Sets, Relations, and Functions (the “thinking tools” behind code)
Learn Computer Mathematics step by step.
All about Computer Mathematics - Sets, Relations, and Functions (the “thinking tools” behind code)
Jan 27, 2026
## Chapter 3: Sets, Relations, and Functions (the “thinking tools” behind code)
A question that comes up early:
“Sir, we already have arrays and lists. Why are you teaching sets? Why are we doing relations and functions like school math?”
Fair question. Because in code, you keep meeting three problems again and again:
1) “I only want unique things. No duplicates.”
2) “I want to describe connections between things.”
3) “I want a rule: given input, produce output.”
Those three problems are exactly what sets, relations, and functions are good at.
No formal definition yet. Keep the idea practical.
### Part 1: Sets (when order doesn’t matter, uniqueness matters)
When you say:
“I have these items, but duplicates should collapse.”
You’re already thinking in sets.
It’s easy to mix up “collection” with “set” at first.
- A list/array feels like a queue of items (order matters, duplicates allowed).
- A set feels like a bag of distinct items (order not the point, duplicates ignored).
That’s why “membership” becomes the main operation:
“Is this element present or not?”
#### Set operations you actually use (without making it scary)
Use one small example and reuse it, so it stays clear.
Imagine two groups:
```text
A = {1, 2, 3}
B = {3, 4, 5}
```
Now ask a few simple questions.
##### Union (A ∪ B): “give me everything”
Union means: take all unique items that appear in A or in B.
```text
A ∪ B = {1, 2, 3, 4, 5}
```
I usually explain union like this: “merge both groups, then remove duplicates.”
##### Intersection (A ∩ B): “give me only common items”
Intersection means: keep only items that are in A and also in B.
```text
A ∩ B = {3}
```
This is the operation behind questions like:
“Which students are present in both batches?”
“Which permissions are common between two roles?”
“Which files are common between two folders?”
##### Difference (A − B): “give me what A has that B doesn’t”
Difference means: start with A, then remove anything that also appears in B.
```text
A − B = {1, 2}
```
Difference trips people because it isn’t symmetric.
```text
B − A = {4, 5}
```
Order matters in difference.
##### One quick real-world picture (permissions)
Think like a developer:
```text
Admin = {read, write, delete}
Editor = {read, write}
```
- `Admin ∩ Editor` gives the shared permissions → `{read, write}`
- `Admin − Editor` gives “extra admin power” → `{delete}`
This is why sets are so natural in programming. You keep comparing groups.
This is not just theory. This is what you do when you compare two groups of users, two lists of permissions, two sets of tags, two sets of files, etc.
### Part 2: Relations (connections, not values)
Now imagine you’re building a tiny social feature:
“Who follows whom?”
That is not a set problem. That is a relation problem. Because it’s about *pairs*.
Like:
(User1 follows User2), (User3 follows User2), (User2 follows User1) …
It’s a bunch of connections.
Relations can feel abstract at first. A simple way to read it is:
“Relation = any rule that tells whether two things are connected.”
That’s it. Nothing mystical.
Then we ask properties like:
- Is the connection symmetric? (if A is connected to B, is B connected to A?)
- Is it transitive? (if A→B and B→C, do we also have A→C?)
These properties matter because they tell you how your program will behave when you “chain” relationships.
### Part 3: Functions (a rule that doesn’t change its mind)
In programming, a function feels like:
Input goes in, output comes out.
In math, it’s the same idea, but with one strict promise:
For the same input, you must get the same output.
In math-thinking, “function” is stricter than “any code block”. It’s more like a reliable mapping.
Like:
- given a roll number, return a student record
- given a product ID, return price
- given a pixel value, return a transformed pixel value
When you break this promise (same input gives different outputs), your debugging becomes painful. Sometimes you want that (randomness, time), but then you must admit you’re adding “extra hidden input”.
#### Basic: unique items is a set idea
You have these tags coming from user input:
```text
["bug", "ui", "bug", "urgent", "ui"]
```
If you think like a set, it becomes:
```text
{"bug", "ui", "urgent"}
```
Notice what disappeared: duplicates.
That’s the whole purpose.
If you’re coding, the mental model is:
“I don’t care how many times the tag appears. I care whether it exists.”
#### Common mistake: expecting sets to preserve order
A common mistake:
They convert a list to a set, then expect it to print in the same order every time.
In many languages, a normal set does not promise stable order. It promises fast membership checks.
So if your use-case is:
“Keep order AND remove duplicates”
Then you need a different tool (like an ordered set structure, or you keep a list plus a “seen” set).
This looks small, but it causes bugs in UI lists and report generation.
#### Simple reason: union and intersection in plain thinking
Let’s say you have two permission groups:
```text
Admin permissions: {read, write, delete}
Editor permissions: {read, write}
```
Union means: give me all permissions that either group has.
```text
{read, write, delete}
```
Intersection means: give me only permissions common to both groups.
```text
{read, write}
```
Why does intersection feel “safe”?
Because it keeps only what both sides agree on.
This is exactly why intersection shows up in security and access control logic.
#### Practical: relations show up as “tables” in real systems
Think of a simple “follows” relation:
```text
(A follows B)
(A follows C)
(C follows B)
```
This relation is not symmetric.
If A follows B, it does not mean B follows A.
Now compare with “is friend of” relation in many apps:
If A is friend of B, usually B is friend of A. That relation is symmetric.
This is not just math talk. It changes how you design your database tables and your API calls.
#### Extra tip: “one-to-one” and “onto” is how you avoid collisions
Suppose you generate short codes for long URLs (like a link shortener).
If two different URLs produce the same short code, you get a collision. Users will open the wrong link.
Thinking in function terms:
- A perfect “one-to-one” mapping would avoid collisions.
- In real systems, we don’t always get perfect one-to-one unless we store extra data or use long enough codes.
So pro thinking becomes:
“If my output space is smaller than my input space, collisions are guaranteed someday.”
This is basically the pigeonhole idea, but we’ll properly talk about that in the combinatorics chapter.
#### Practical + slightly advanced: programming functions vs math functions
In math-thinking, a function like:
```text
f(x) = x + 1
```
always gives the same output for the same input.
But in programming, people write:
```text
getDiscountedPrice(productId)
```
and inside it uses “current time”, “festival sale”, “user membership”, “coupon usage”.
So the output changes.
That doesn’t make it “bad”. It just means:
Your input is not only `productId`. Your input secretly includes system state.
Once you start seeing hidden inputs, you start debugging like a professional.
---
## Conclusion
In this article, we explored the core concepts of All about Computer Mathematics - Sets, Relations, and Functions (the “thinking tools” behind code). Understanding these fundamentals is crucial for any developer looking to master this topic.
## Frequently Asked Questions (FAQs)
**Q: What is All about Computer Mathematics - Sets, Relations, and Functions (the “thinking tools” behind code)?**
A: All about Computer Mathematics - Sets, Relations, and Functions (the “thinking tools” behind code) is a fundamental concept in this programming language/topic that allows developers to perform specific tasks efficiently.
**Q: Why is All about Computer Mathematics - Sets, Relations, and Functions (the “thinking tools” behind code) important?**
A: It helps in organizing code, improving performance, and implementing complex logic in a structured way.
**Q: How to get started with All about Computer Mathematics - Sets, Relations, and Functions (the “thinking tools” behind code)?**
A: You can start by practicing the basic syntax and examples provided in this tutorial.
**Q: Are there any prerequisites for All about Computer Mathematics - Sets, Relations, and Functions (the “thinking tools” behind code)?**
A: Basic knowledge of programming logic and syntax is recommended.
**Q: Can All about Computer Mathematics - Sets, Relations, and Functions (the “thinking tools” behind code) be used in real-world projects?**
A: Yes, it is widely used in enterprise-level applications and software development.
**Q: Where can I find more examples of All about Computer Mathematics - Sets, Relations, and Functions (the “thinking tools” behind code)?**
A: You can check our blog section for more advanced tutorials and use cases.
**Q: Is All about Computer Mathematics - Sets, Relations, and Functions (the “thinking tools” behind code) suitable for beginners?**
A: Yes, our guide is designed to be beginner-friendly with clear explanations.
**Q: How does All about Computer Mathematics - Sets, Relations, and Functions (the “thinking tools” behind code) improve code quality?**
A: By providing a standardized way to handle logic, it makes code more readable and maintainable.
**Q: What are common mistakes when using All about Computer Mathematics - Sets, Relations, and Functions (the “thinking tools” behind code)?**
A: Common mistakes include incorrect syntax usage and not following best practices, which we've covered here.
**Q: Does this tutorial cover advanced All about Computer Mathematics - Sets, Relations, and Functions (the “thinking tools” behind code)?**
A: This article covers the essentials; stay tuned for our advanced series on this topic.