Learn CHAPEL with Real Code Examples
Updated Nov 20, 2025
Code Sample Descriptions
1
Chapel Counter and Theme Toggle
var count: int = 0;
var isDark: bool = false;
proc updateUI() {
writeln("Counter: ", count);
writeln("Theme: ", if isDark then "Dark" else "Light");
}
proc increment() {
count += 1;
updateUI();
}
proc decrement() {
count -= 1;
updateUI();
}
proc reset() {
count = 0;
updateUI();
}
proc toggleTheme() {
isDark = !isDark;
updateUI();
}
// Simulate actions
updateUI();
increment();
increment();
toggleTheme();
decrement();
reset();
Demonstrates a simple counter with theme toggling using Chapel variables, procedures, and I/O.
2
Chapel Fibonacci Sequence
var a: int = 0;
var b: int = 1;
writeln(a);
writeln(b);
for i in 1..8 {
var c = a + b;
writeln(c);
a = b;
b = c;
}
Generates first 10 Fibonacci numbers using an iterative approach.
3
Chapel Factorial Calculator
proc factorial(n: int): int {
if n == 0 then return 1;
return n * factorial(n-1);
}
writeln(factorial(5));
Calculates factorial of a number using recursion.
4
Chapel Prime Checker
proc isPrime(n: int): bool {
if n < 2 then return false;
for i in 2..n-1 {
if n % i == 0 then return false;
}
return true;
}
writeln(if isPrime(13) then "Prime" else "Not Prime");
Checks if a number is prime.
5
Chapel Sum of Array
var nums = [1,2,3,4,5];
var sum = 0;
for n in nums do sum += n;
writeln(sum);
Calculates sum of an array of numbers.
6
Chapel Reverse String
var s = "HELLO";
var r = s.reversed();
writeln(r);
Reverses a string.
7
Chapel Multiplication Table
var n = 5;
for i in 1..10 do writeln(n, " x ", i, " = ", n*i);
Prints multiplication table of a number.
8
Chapel Celsius to Fahrenheit
var c: real = 25.0;
var f = (c * 9.0/5.0) + 32.0;
writeln(f);
Converts Celsius to Fahrenheit.
9
Chapel Simple Alarm Simulation
var temp = 80;
var thresh = 75;
writeln(if temp > thresh then "Alarm: Temperature Too High!" else "Temperature Normal");
Simulates an alarm if a threshold is exceeded.
10
Chapel Random Walk Simulation
var steps = 10;
var pos = 0;
for i in 1..steps {
if random(0,1) < 0.5 then pos += 1 else pos -= 1;
writeln(pos);
}
Simulates a 1D random walk.