Show description
Recursion Across 20 Languages | CSE 240 Guide
Recursion Across 20 Languages | CSE 240 Guide
Languages
Python
JavaScript
C
C++
Java
C#
TypeScript
SQL
Go
Swift
Kotlin
R
PHP
Ruby
Rust
Dart
MATLAB
Scala
Perl
Bash
Recursive Functions Across 20 Languages
A comprehensive guide for CSE 240 and beyond
Python
Quick Reference
Variables
x = 10 name = "value" (dynamic typing)
Functions
def function_name(param): followed by indented body
Classes
class ClassName: with def __init__(self): for constructor
Comments
# single line or """multi-line"""
Control Flow
if condition: elif: else: for item in list: while condition:
Recursion Example: Study Hours Calculator
def calculate_study_hours(module_number):
# Base case: Module 1 takes a baseline of 3 hours.
if module_number == 1:
return 3
# Recursive step: Each subsequent module takes its number in hours,
# plus all the hours from previous modules.
else:
return module_number + calculate_study_hours(module_number - 1)
# How many hours for the first 4 modules?
print(calculate_study_hours(4)) # Output: 10 (4 + 3 + 2 + 1)
JavaScript
Quick Reference
Variables
let x = 10; const y = 20; var z = 30; (use let/const)
Functions
function name(param) { } or const name = (param) => { }
Classes
class Name { constructor() { } method() { } }
Comments
// single line or /* multi-line */
Control Flow
if (condition) { } for (let i = 0; i < n; i++) { } while (condition) { }
Recursion Example: Concept Unlocker
function unlockConcept(currentConceptID, finalConceptID) {
// Base case: You've reached the final concept for the week.
if (currentConceptID >= finalConceptID) {
console.log(`Concept ${finalConceptID} mastered! Module complete.`);
return;
}
// Recursive step: Study the current concept and move to the next.
console.log(`Studying concept ${currentConceptID}...`);
unlockConcept(currentConceptID + 1, finalConceptID);
}
unlockConcept(1, 5);
C
Quick Reference
Variables
int x = 10; char c = 'a'; float f = 3.14; (static typing)
Functions
return_type function_name(type param) { }
Pointers
int *ptr = &x; *ptr (dereference) &var (address)
Structs
struct Name { type field; }; struct Name var;
Memory
malloc…
Recursion Across 20 Languages | CSE 240 Guide
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Recursion Across 20 Languages | CSE 240 Guide</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: #0a0a0a;
color: #e0e0e0;
line-height: 1.6;
}
.container {
display: flex;
min-height: 100vh;
}
.sidebar {
width: 250px;
background: #1a1a1a;
border-right: 2px solid #ff6b35;
padding: 20px;
position: sticky;
top: 0;
height: 100vh;
overflow-y: auto;
}
.sidebar h1 {
color: #ff6b35;
font-size: 1.5em;
margin-bottom: 20px;
padding-bottom: 10px;
border-bottom: 2px solid #ff6b35;
}
.sidebar nav ul {
list-style: none;
}
.sidebar nav li {
margin: 8px 0;
}
.sidebar nav a {
color: #b0b0b0;
text-decoration: none;
display: block;
padding: 8px 12px;
border-radius: 4px;
transition: all 0.3s;
}
.sidebar nav a:hover {
background: #2a2a2a;
color: #ff6b35;
transform: translateX(5px);
}
.main-content {
flex: 1;
padding: 40px;
max-width: 1200px;
}
.header {
margin-bottom: 40px;
padding-bottom: 20px;
border-bottom: 3px solid #ff6b35;
}
.header h1 {
color: #ff6b35;
font-size: 2.5em;
margin-bottom: 10px;
}
.header p {
color: #888;
font-size: 1.1em;
}
.language-section {
margin-bottom: 60px;
background: #1a1a1a;
border-radius: 8px;
padding: 30px;
border-left: 4px solid #ff6b35;
}
.language-section h2 {
color: #ff6b35;
font-size: 2em;
margin-bottom: 20px;
}
.section-block {
margin-bottom: 25px;
}
.section-block h3 {
color: #ff8c5a;
font-size: 1.3em;
margin-bottom: 15px;
padding-bottom: 8px;
border-bottom: 1px solid #333;
}
.cheat-sheet {
background: #0f0f0f;
padding: 20px;
border-radius: 6px;
border: 1px solid #333;
}
.cheat-item {
margin-bottom: 15px;
}
.cheat-item h4 {
color: #ff6b35;
font-size: 1em;
margin-bottom: 8px;
}
.cheat-item code {
background: #1a1a1a;
color: #ffa366;
padding: 2px 6px;
border-radius: 3px;
font-family: 'Courier New', monospace;
}
pre {
background: #0f0f0f;
border: 1px solid #333;
border-radius: 6px;
padding: 20px;
overflow-x: auto;
margin: 15px 0;
}
code {
font-family: 'Courier New', monospace;
font-size: 0.95em;
line-height: 1.5;
}
.keyword { color: #ff6b35; }
.function { color: #4fc3f7; }
.string { color: #98c379; }
.comment { color: #666; font-style: italic; }
.number { color: #d19a66; }
@media (max-width: 768px) {
.container {
flex-direction: column;
}
.sidebar {
width: 100%;
height: auto;
position: relative;
}
.main-content {
padding: 20px;
}
}
::-webkit-scrollbar {
width: 10px;
}
::-webkit-scrollbar-track {
background: #1a1a1a;
}
::-webkit-scrollbar-thumb {
background: #ff6b35;
border-radius: 5px;
}
::-webkit-scrollbar-thumb:hover {
background: #ff8c5a;
}
</style>
</head>
<body>
<div class="container">
<aside class="sidebar">
<h1>Languages</h1>
<nav>
<ul>
<li><a href="#python">Python</a></li>
<li><a href="#javascript">JavaScript</a></li>
<li><a href="#c">C</a></li>
<li><a href="#cpp">C++</a></li>
<li><a href="#java">Java</a></li>
<li><a href="#csharp">C#</a></li>
<li><a href="#typescript">TypeScript</a></li>
<li><a href="#sql">SQL</a></li>
<li><a href="#go">Go</a></li>
<li><a href="#swift">Swift</a></li>
<li><a href="#kotlin">Kotlin</a></li>
<li><a href="#r">R</a></li>
<li><a href="#php">PHP</a></li>
<li><a href="#ruby">Ruby</a></li>
<li><a href="#rust">Rust</a></li>
<li><a href="#dart">Dart</a></li>
<li><a href="#matlab">MATLAB</a></li>
<li><a href="#scala">Scala</a></li>
<li><a href="#perl">Perl</a></li>
<li><a href="#bash">Bash</a></li>
</ul>
</nav>
</aside>
<main class="main-content">
<div class="header">
<h1>Recursive Functions Across 20 Languages</h1>
<p>A comprehensive guide for CSE 240 and beyond</p>
</div>
<!-- PYTHON -->
<section id="python" class="language-section">
<h2>Python</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Variables</h4>
<code>x = 10</code> <code>name = "value"</code> (dynamic typing)
</div>
<div class="cheat-item">
<h4>Functions</h4>
<code>def function_name(param):</code> followed by indented body
</div>
<div class="cheat-item">
<h4>Classes</h4>
<code>class ClassName:</code> with <code>def __init__(self):</code> for constructor
</div>
<div class="cheat-item">
<h4>Comments</h4>
<code># single line</code> or <code>"""multi-line"""</code>
</div>
<div class="cheat-item">
<h4>Control Flow</h4>
<code>if condition:</code> <code>elif:</code> <code>else:</code> <code>for item in list:</code> <code>while condition:</code>
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: Study Hours Calculator</h3>
<pre><code>def calculate_study_hours(module_number):
# Base case: Module 1 takes a baseline of 3 hours.
if module_number == 1:
return 3
# Recursive step: Each subsequent module takes its number in hours,
# plus all the hours from previous modules.
else:
return module_number + calculate_study_hours(module_number - 1)
# How many hours for the first 4 modules?
print(calculate_study_hours(4)) # Output: 10 (4 + 3 + 2 + 1)</code></pre>
</div>
</section>
<!-- JAVASCRIPT -->
<section id="javascript" class="language-section">
<h2>JavaScript</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Variables</h4>
<code>let x = 10;</code> <code>const y = 20;</code> <code>var z = 30;</code> (use let/const)
</div>
<div class="cheat-item">
<h4>Functions</h4>
<code>function name(param) { }</code> or <code>const name = (param) => { }</code>
</div>
<div class="cheat-item">
<h4>Classes</h4>
<code>class Name { constructor() { } method() { } }</code>
</div>
<div class="cheat-item">
<h4>Comments</h4>
<code>// single line</code> or <code>/* multi-line */</code>
</div>
<div class="cheat-item">
<h4>Control Flow</h4>
<code>if (condition) { }</code> <code>for (let i = 0; i < n; i++) { }</code> <code>while (condition) { }</code>
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: Concept Unlocker</h3>
<pre><code>function unlockConcept(currentConceptID, finalConceptID) {
// Base case: You've reached the final concept for the week.
if (currentConceptID >= finalConceptID) {
console.log(`Concept ${finalConceptID} mastered! Module complete.`);
return;
}
// Recursive step: Study the current concept and move to the next.
console.log(`Studying concept ${currentConceptID}...`);
unlockConcept(currentConceptID + 1, finalConceptID);
}
unlockConcept(1, 5);</code></pre>
</div>
</section>
<!-- C -->
<section id="c" class="language-section">
<h2>C</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Variables</h4>
<code>int x = 10;</code> <code>char c = 'a';</code> <code>float f = 3.14;</code> (static typing)
</div>
<div class="cheat-item">
<h4>Functions</h4>
<code>return_type function_name(type param) { }</code>
</div>
<div class="cheat-item">
<h4>Pointers</h4>
<code>int *ptr = &x;</code> <code>*ptr</code> (dereference) <code>&var</code> (address)
</div>
<div class="cheat-item">
<h4>Structs</h4>
<code>struct Name { type field; };</code> <code>struct Name var;</code>
</div>
<div class="cheat-item">
<h4>Memory</h4>
<code>malloc(size)</code> <code>free(ptr)</code> <code>sizeof(type)</code>
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: Pointer Tracer</h3>
<pre><code>#include <stdio.h>
// Imagine this function traces memory addresses.
void trace_pointer(int pointer_level) {
// Base case: We've reached the actual data.
if (pointer_level <= 0) {
printf("Reached the value!\n");
return;
}
// Recursive step: "Dereference" and go to the next level.
printf("Level %d pointer -> \n", pointer_level);
trace_pointer(pointer_level - 1);
}
int main() {
trace_pointer(3); // Simulate a pointer-to-pointer-to-int (int***)
return 0;
}</code></pre>
</div>
</section>
<!-- C++ -->
<section id="cpp" class="language-section">
<h2>C++</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Variables</h4>
<code>int x = 10;</code> <code>auto y = 5;</code> (type inference)
</div>
<div class="cheat-item">
<h4>Functions</h4>
<code>return_type name(type param) { }</code> references: <code>void func(int &ref)</code>
</div>
<div class="cheat-item">
<h4>Classes</h4>
<code>class Name { public: Name(); void method(); };</code>
</div>
<div class="cheat-item">
<h4>Namespaces</h4>
<code>using namespace std;</code> or <code>std::cout</code>
</div>
<div class="cheat-item">
<h4>Templates</h4>
<code>template<typename T> T func(T param) { }</code>
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: Project Compiler</h3>
<pre><code>#include <iostream>
void compile_project(int remaining_files) {
// Base case: No files left to compile.
if (remaining_files <= 0) {
std::cout << "Project compiled successfully!" << std::endl;
return;
}
// Recursive step: Compile one file and reduce the count.
std::cout << "Compiling file " << remaining_files << "..." << std::endl;
compile_project(remaining_files - 1);
}
int main() {
compile_project(5);
return 0;
}</code></pre>
</div>
</section>
<!-- JAVA -->
<section id="java" class="language-section">
<h2>Java</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Variables</h4>
<code>int x = 10;</code> <code>String s = "text";</code> (static typing)
</div>
<div class="cheat-item">
<h4>Functions</h4>
<code>public static return_type name(type param) { }</code>
</div>
<div class="cheat-item">
<h4>Classes</h4>
<code>public class Name { public Name() { } public void method() { } }</code>
</div>
<div class="cheat-item">
<h4>Access</h4>
<code>public</code> <code>private</code> <code>protected</code> <code>default</code>
</div>
<div class="cheat-item">
<h4>Keywords</h4>
<code>static</code> <code>final</code> <code>abstract</code> <code>extends</code> <code>implements</code>
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: Bug Debugger</h3>
<pre><code>public class Debugger {
public static void debugCode(int bugsRemaining) {
// Base case: No bugs left! Time to submit.
if (bugsRemaining <= 0) {
System.out.println("All bugs fixed. Code is clean!");
return;
}
// Recursive step: Fix a bug and check the rest of the code.
System.out.println("Squashing bug #" + bugsRemaining);
debugCode(bugsRemaining - 1);
}
public static void main(String[] args) {
debugCode(4);
}
}</code></pre>
</div>
</section>
<!-- C# -->
<section id="csharp" class="language-section">
<h2>C#</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Variables</h4>
<code>int x = 10;</code> <code>var y = "auto";</code> <code>const int Z = 5;</code>
</div>
<div class="cheat-item">
<h4>Functions</h4>
<code>public static return_type Name(type param) { }</code>
</div>
<div class="cheat-item">
<h4>Classes</h4>
<code>public class Name { public Name() { } public void Method() { } }</code>
</div>
<div class="cheat-item">
<h4>Properties</h4>
<code>public int Prop { get; set; }</code>
</div>
<div class="cheat-item">
<h4>LINQ</h4>
<code>list.Where(x => x > 5).Select(x => x * 2)</code>
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: Topic Master</h3>
<pre><code>using System;
public class Student {
public static void MasterTopic(int difficulty) {
// Base case: The topic is easy enough to understand.
if (difficulty <= 10) {
Console.WriteLine("Topic mastered!");
return;
}
// Recursive step: Study to reduce difficulty and try again.
Console.WriteLine($"Topic difficulty is {difficulty}. Reviewing...");
MasterTopic(difficulty - 15); // Each session helps!
}
public static void Main(string[] args) {
MasterTopic(50); // Start with a tough topic.
}
}</code></pre>
</div>
</section>
<!-- TYPESCRIPT -->
<section id="typescript" class="language-section">
<h2>TypeScript</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Variables</h4>
<code>let x: number = 10;</code> <code>const y: string = "text";</code>
</div>
<div class="cheat-item">
<h4>Functions</h4>
<code>function name(param: type): returnType { }</code>
</div>
<div class="cheat-item">
<h4>Interfaces</h4>
<code>interface Name { prop: type; method(): type; }</code>
</div>
<div class="cheat-item">
<h4>Types</h4>
<code>type Name = string | number;</code> <code>type Obj = { x: number }</code>
</div>
<div class="cheat-item">
<h4>Generics</h4>
<code>function name<T>(param: T): T { }</code>
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: Assignment Validator</h3>
<pre><code>function validateAssignment(checksLeft: number): void {
// Base case: All checks passed.
if (checksLeft <= 0) {
console.log('Assignment is valid and ready to submit!');
return;
}
// Recursive step: Perform a check and move to the next.
console.log(`Running check #${checksLeft}... OK.`);
validateAssignment(checksLeft - 1);
}
validateAssignment(3);</code></pre>
</div>
</section>
<!-- SQL -->
<section id="sql" class="language-section">
<h2>SQL</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Basic Queries</h4>
<code>SELECT columns FROM table WHERE condition;</code>
</div>
<div class="cheat-item">
<h4>CRUD</h4>
<code>INSERT INTO</code> <code>UPDATE table SET</code> <code>DELETE FROM</code>
</div>
<div class="cheat-item">
<h4>Joins</h4>
<code>INNER JOIN</code> <code>LEFT JOIN</code> <code>RIGHT JOIN</code> <code>ON condition</code>
</div>
<div class="cheat-item">
<h4>Aggregates</h4>
<code>COUNT()</code> <code>SUM()</code> <code>AVG()</code> <code>GROUP BY</code> <code>HAVING</code>
</div>
<div class="cheat-item">
<h4>CTEs</h4>
<code>WITH name AS (SELECT ...) SELECT * FROM name;</code>
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: Course Prerequisites Path</h3>
<pre><code>-- Create a table of course prerequisites
CREATE TABLE Courses (id TEXT, prereq_id TEXT);
INSERT INTO Courses VALUES ('CSE100', NULL), ('CSE120', 'CSE100'), ('CSE240', 'CSE120');
-- Recursive Query to find the full path
WITH RECURSIVE course_path(id, path) AS (
SELECT id, id FROM Courses WHERE id = 'CSE100' -- Base case
UNION ALL
SELECT c.id, p.path || ' -> ' || c.id -- Recursive step
FROM Courses c, course_path p
WHERE c.prereq_id = p.id
)
SELECT path FROM course_path WHERE id = 'CSE240';
-- Result: "CSE100 -> CSE120 -> CSE240"</code></pre>
</div>
</section>
<!-- GO -->
<section id="go" class="language-section">
<h2>Go</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Variables</h4>
<code>var x int = 10</code> or <code>x := 10</code> (short declaration)
</div>
<div class="cheat-item">
<h4>Functions</h4>
<code>func name(param type) returnType { }</code>
</div>
<div class="cheat-item">
<h4>Structs</h4>
<code>type Name struct { field type }</code>
</div>
<div class="cheat-item">
<h4>Methods</h4>
<code>func (receiver Type) methodName() { }</code>
</div>
<div class="cheat-item">
<h4>Goroutines</h4>
<code>go functionCall()</code> <code>ch := make(chan type)</code>
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: Lab Task Completer</h3>
<pre><code>package main
import "fmt"
func completeLab(taskID int) {
// Base case: Last task is done.
if taskID <= 0 {
fmt.Println("Lab project complete!")
return
}
// Recursive step: Finish a task and start the next.
fmt.Printf("Completing task #%d...\n", taskID)
completeLab(taskID - 1)
}
func main() {
completeLab(4)
}</code></pre>
</div>
</section>
<!-- SWIFT -->
<section id="swift" class="language-section">
<h2>Swift</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Variables</h4>
<code>var x = 10</code> (mutable) <code>let y = 20</code> (immutable)
</div>
<div class="cheat-item">
<h4>Functions</h4>
<code>func name(param: Type) -> ReturnType { }</code>
</div>
<div class="cheat-item">
<h4>Classes/Structs</h4>
<code>class Name { init() { } func method() { } }</code>
</div>
<div class="cheat-item">
<h4>Optionals</h4>
<code>var x: Int?</code> <code>if let x = optional { }</code> <code>x ?? default</code>
</div>
<div class="cheat-item">
<h4>Closures</h4>
<code>{ (params) -> ReturnType in body }</code>
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: Exam Prep Countdown</h3>
<pre><code>func prepareForExam(daysLeft: Int) {
// Base case: It's exam day!
if daysLeft <= 0 {
print("Good luck on the exam!")
return
}
// Recursive step: Study for today and decrement the day count.
print("\(daysLeft) days left. Time for a study session!")
prepareForExam(daysLeft: daysLeft - 1)
}
prepareForExam(daysLeft: 3)</code></pre>
</div>
</section>
<!-- KOTLIN -->
<section id="kotlin" class="language-section">
<h2>Kotlin</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Variables</h4>
<code>var x = 10</code> (mutable) <code>val y = 20</code> (immutable)
</div>
<div class="cheat-item">
<h4>Functions</h4>
<code>fun name(param: Type): ReturnType { }</code>
</div>
<div class="cheat-item">
<h4>Classes</h4>
<code>class Name(val prop: Type) { fun method() { } }</code>
</div>
<div class="cheat-item">
<h4>Null Safety</h4>
<code>var x: Int?</code> <code>x?.method()</code> <code>x ?: default</code> <code>x!!</code>
</div>
<div class="cheat-item">
<h4>Extensions</h4>
<code>fun Type.newMethod() { }</code>
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: Problem Set Solver</h3>
<pre><code>fun solveProblemSet(problemsLeft: Int) {
// Base case: No more problems to solve.
if (problemsLeft <= 0) {
println("Assignment finished!")
return
}
// Recursive step: Solve one problem, then tackle the rest.
println("Solving problem #$problemsLeft...")
solveProblemSet(problemsLeft - 1)
}
fun main() {
solveProblemSet(5)
}</code></pre>
</div>
</section>
<!-- R -->
<section id="r" class="language-section">
<h2>R</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Variables</h4>
<code>x <- 10</code> or <code>x = 10</code> (assignment)
</div>
<div class="cheat-item">
<h4>Functions</h4>
<code>name <- function(param) { body }</code>
</div>
<div class="cheat-item">
<h4>Vectors</h4>
<code>c(1, 2, 3)</code> <code>1:10</code> <code>seq(1, 10, 2)</code>
</div>
<div class="cheat-item">
<h4>Data Frames</h4>
<code>df <- data.frame(col1 = c(...), col2 = c(...))</code>
</div>
<div class="cheat-item">
<h4>Conditionals</h4>
<code>if (condition) { } else { }</code> <code>ifelse(test, yes, no)</code>
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: Dataset Analyzer</h3>
<pre><code>analyze_dataset <- function(data_points) {
# Base case: The dataset is small enough for a final conclusion.
if (data_points < 10) {
return("Insight found!")
}
# Recursive step: Subset the data and analyze the smaller piece.
cat("Analyzing", data_points, "points. Subsetting...\n")
analyze_dataset(data_points / 2)
}
analyze_dataset(100)</code></pre>
</div>
</section>
<!-- PHP -->
<section id="php" class="language-section">
<h2>PHP</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Variables</h4>
<code>$x = 10;</code> (all variables start with $)
</div>
<div class="cheat-item">
<h4>Functions</h4>
<code>function name($param) { return $value; }</code>
</div>
<div class="cheat-item">
<h4>Classes</h4>
<code>class Name { public function __construct() { } }</code>
</div>
<div class="cheat-item">
<h4>Arrays</h4>
<code>$arr = array(1, 2, 3);</code> or <code>$arr = [1, 2, 3];</code>
</div>
<div class="cheat-item">
<h4>Output</h4>
<code>echo "text";</code> <code>print "text";</code> <code>var_dump($var);</code>
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: Study Guide Generator</h3>
<pre><code><?php
function generate_study_guide($topics) {
// Base case: No topics left in the array.
if (empty($topics)) {
return;
}
// Recursive step: Print the first topic and process the rest.
$current_topic = array_shift($topics);
echo "<li>$current_topic</li>";
generate_study_guide($topics);
}
$module_topics = ['Pointers', 'Structs', 'File I/O'];
echo "<ul>";
generate_study_guide($module_topics);
echo "</ul>";
?></code></pre>
</div>
</section>
<!-- RUBY -->
<section id="ruby" class="language-section">
<h2>Ruby</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Variables</h4>
<code>x = 10</code> (local) <code>@x</code> (instance) <code>@@x</code> (class) <code>$x</code> (global)
</div>
<div class="cheat-item">
<h4>Functions/Methods</h4>
<code>def name(param) body end</code>
</div>
<div class="cheat-item">
<h4>Classes</h4>
<code>class Name; def initialize; end; end</code>
</div>
<div class="cheat-item">
<h4>Blocks</h4>
<code>{ |param| body }</code> or <code>do |param| body end</code>
</div>
<div class="cheat-item">
<h4>Arrays/Hashes</h4>
<code>[1, 2, 3]</code> <code>{key: value}</code> <code>.each</code> <code>.map</code>
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: Flashcard Reviewer</h3>
<pre><code>def review_flashcards(cards)
# Base case: No cards left to review.
if cards.empty?
puts "All cards reviewed!"
return
end
# Recursive step: Review the top card, then the rest of the deck.
current_card = cards.shift
puts "Reviewing: #{current_card}"
review_flashcards(cards)
end
flashcards = ["Paradigm", "Pointer", "Recursion"]
review_flashcards(flashcards)</code></pre>
</div>
</section>
<!-- RUST -->
<section id="rust" class="language-section">
<h2>Rust</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Variables</h4>
<code>let x = 10;</code> (immutable) <code>let mut y = 20;</code> (mutable)
</div>
<div class="cheat-item">
<h4>Functions</h4>
<code>fn name(param: type) -> ReturnType { }</code>
</div>
<div class="cheat-item">
<h4>Structs</h4>
<code>struct Name { field: Type }</code> <code>impl Name { fn method(&self) { } }</code>
</div>
<div class="cheat-item">
<h4>Ownership</h4>
<code>&x</code> (borrow) <code>&mut x</code> (mutable borrow) <code>move</code>
</div>
<div class="cheat-item">
<h4>Error Handling</h4>
<code>Result<T, E></code> <code>Option<T></code> <code>match</code> <code>?</code> operator
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: Memory Safety Checker</h3>
<pre><code>fn check_memory_safety(block_id: u32) {
// Base case: Innermost block is reached and is safe.
if block_id == 0 {
println!("All blocks are memory safe.");
return;
}
// Recursive step: Verify current block and check its parent.
println!("Checking block {} for safety... OK.", block_id);
check_memory_safety(block_id - 1);
}
fn main() {
check_memory_safety(3);
}</code></pre>
</div>
</section>
<!-- DART -->
<section id="dart" class="language-section">
<h2>Dart</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Variables</h4>
<code>var x = 10;</code> <code>int y = 20;</code> <code>final z = 30;</code> <code>const W = 40;</code>
</div>
<div class="cheat-item">
<h4>Functions</h4>
<code>ReturnType name(Type param) { }</code> or <code>name(param) => expression;</code>
</div>
<div class="cheat-item">
<h4>Classes</h4>
<code>class Name { Name(); void method() { } }</code>
</div>
<div class="cheat-item">
<h4>Null Safety</h4>
<code>int? x;</code> <code>x!</code> (assert non-null) <code>x ?? default</code>
</div>
<div class="cheat-item">
<h4>Async</h4>
<code>Future<T></code> <code>async { }</code> <code>await</code>
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: UI Widget Builder</h3>
<pre><code>void buildUiWidget(int complexity) {
// Base case: The simplest widget needs no more nesting.
if (complexity <= 1) {
print(' Building final Text Widget...');
return;
}
// Recursive step: Build a container and nest a simpler widget inside.
print('Building Container Widget of complexity $complexity...');
buildUiWidget(complexity - 1);
}
void main() {
buildUiWidget(3);
}</code></pre>
</div>
</section>
<!-- MATLAB -->
<section id="matlab" class="language-section">
<h2>MATLAB</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Variables</h4>
<code>x = 10;</code> <code>A = [1 2; 3 4];</code> (matrices)
</div>
<div class="cheat-item">
<h4>Functions</h4>
<code>function output = name(input) body end</code>
</div>
<div class="cheat-item">
<h4>Indexing</h4>
<code>A(1,2)</code> (1-indexed) <code>A(:,1)</code> (all rows, col 1) <code>A(end,:)</code>
</div>
<div class="cheat-item">
<h4>Operators</h4>
<code>.*</code> <code>./</code> <code>.^</code> (element-wise) <code>*</code> (matrix mult)
</div>
<div class="cheat-item">
<h4>Plotting</h4>
<code>plot(x, y)</code> <code>xlabel('text')</code> <code>title('text')</code>
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: Matrix Determinant</h3>
<pre><code>function det_val = simple_recursive_determinant(matrix)
% NOTE: This is a simplified example for a 3x3 matrix.
[rows, ~] = size(matrix);
% Base case: The determinant of a 2x2 matrix.
if rows == 2
det_val = matrix(1,1)*matrix(2,2) - matrix(1,2)*matrix(2,1);
return;
end
% Recursive step (simplified for first element expansion)
sub_matrix = matrix(2:end, 2:end);
det_val = matrix(1,1) * simple_recursive_determinant(sub_matrix);
end</code></pre>
</div>
</section>
<!-- SCALA -->
<section id="scala" class="language-section">
<h2>Scala</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Variables</h4>
<code>val x = 10</code> (immutable) <code>var y = 20</code> (mutable)
</div>
<div class="cheat-item">
<h4>Functions</h4>
<code>def name(param: Type): ReturnType = { }</code>
</div>
<div class="cheat-item">
<h4>Classes</h4>
<code>class Name(val field: Type) { def method = { } }</code>
</div>
<div class="cheat-item">
<h4>Pattern Matching</h4>
<code>x match { case pattern => result }</code>
</div>
<div class="cheat-item">
<h4>Collections</h4>
<code>List(1, 2, 3)</code> <code>.map</code> <code>.filter</code> <code>.fold</code>
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: Topic Processor</h3>
<pre><code>object Course {
def processTopics(topics: List[String]): Unit = topics match {
// Base case: The list of topics is empty.
case Nil => println("All topics processed.")
// Recursive step: Process the head and recur on the tail.
case head :: tail =>
println(s"Processing: $head")
processTopics(tail)
}
def main(args: Array[String]): Unit = {
val moduleTopics = List("Scheme", "Prolog", "OOP")
processTopics(moduleTopics)
}
}</code></pre>
</div>
</section>
<!-- PERL -->
<section id="perl" class="language-section">
<h2>Perl</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Variables</h4>
<code>$scalar</code> <code>@array</code> <code>%hash</code>
</div>
<div class="cheat-item">
<h4>Subroutines</h4>
<code>sub name { my ($param) = @_; }</code>
</div>
<div class="cheat-item">
<h4>Arrays</h4>
<code>push @arr, $val;</code> <code>pop @arr;</code> <code>shift</code> <code>unshift</code>
</div>
<div class="cheat-item">
<h4>Regex</h4>
<code>$str =~ /pattern/;</code> <code>$str =~ s/old/new/;</code>
</div>
<div class="cheat-item">
<h4>File I/O</h4>
<code>open(my $fh, '<', $file);</code> <code>while (<$fh>) { }</code>
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: Log File Parser</h3>
<pre><code>sub parse_log_file {
my ($line_number) = @_;
# Base case: We've reached the end of the log snippet.
if ($line_number > 3) {
print "Log file parsed.\n";
return;
}
# Recursive step: Print a log line and move to the next.
print "Parsing log line $line_number...\n";
parse_log_file($line_number + 1);
}
parse_log_file(1);</code></pre>
</div>
</section>
<!-- BASH -->
<section id="bash" class="language-section">
<h2>Bash</h2>
<div class="section-block">
<h3>Quick Reference</h3>
<div class="cheat-sheet">
<div class="cheat-item">
<h4>Variables</h4>
<code>VAR="value"</code> <code>$VAR</code> or <code>${VAR}</code> (no spaces!)
</div>
<div class="cheat-item">
<h4>Functions</h4>
<code>function_name() { commands; }</code>
</div>
<div class="cheat-item">
<h4>Arguments</h4>
<code>$1, $2, ...</code> <code>$@</code> (all args) <code>$#</code> (count)
</div>
<div class="cheat-item">
<h4>Conditionals</h4>
<code>if [[ condition ]]; then fi</code> <code>-eq -ne -lt -gt</code> (numbers) <code>= !=</code> (strings)
</div>
<div class="cheat-item">
<h4>Loops</h4>
<code>for i in {1..10}; do done</code> <code>while [[ condition ]]; do done</code>
</div>
</div>
</div>
<div class="section-block">
<h3>Recursion Example: Project File Organizer</h3>
<pre><code>#!/bin/bash
organize_project_files() {
local level=$1
local prefix=$2
# Base case: Stop creating directories after a certain depth.
if [[ $level -le 0 ]]; then
return
fi
# Recursive step: Create a directory and then create a sub-directory.
local dir_name="${prefix}_level_${level}"
mkdir "$dir_name"
echo "Created directory: $dir_name"
cd "$dir_name"
organize_project_files $((level - 1)) "sub"
cd ..
}
organize_project_files 2 "module"</code></pre>
</div>
</section>
</main>
</div>
<script>
// Smooth scrolling for navigation links
document.querySelectorAll('.sidebar a').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
target.scrollIntoView({ behavior: 'smooth', block: 'start' });
});
});
</script>
</body>
</html>