3 A Tour of Zeno
This chapter runs through the whole language quickly, one small program at a time, so that you know what is coming and where it is explained. Nothing here has to be understood yet. Each section names the chapter that goes through it properly.
3.1 Values that change, and values that do not
let name = "Zeno" # let: set once, never changes
var count = 3 # var: can change later
count = count + 1 # plain = changes a var
const LIMIT = 100 # const: fixed when the program is built
print(name, count, LIMIT)Zeno 4 100
A name is declared once, with let, var or
const, and from then on plain = changes it. A
misspelt name is therefore a mistake, and never a new variable. (Values and Variables.)
3.2 Lists, and a comprehension
let readings = [21.5, 22.0, -99.0, 21.0, 150.0]
let kept = [r for r in readings if -40.0 <= r <= 85.0]
print(kept, sum(kept) / len(kept))[21.5, 22.0, 21.0] 21.5
Comparisons chain as they do in mathematics:
-40.0 <= r <= 85.0 means what it looks like. (Lists, Numbers and Operators.)
3.3 Functions, with types or without
function describe(t: float) -> str {
if t < 18.0 {
return "cold"
} else if t < 24.0 {
return "comfortable"
}
return "warm"
}
function average(values) {
return sum(values) / len(values)
}
for t in [12.0, 21.5, 30.0] {
print(t, describe(t))
}
print(average([1, 2, 3, 4]))12.0 cold
21.5 comfortable
30.0 warm
2.5
Types are optional. Where they are written, they are checked before the program runs. Where they are left out, Zeno works them out from how the function is used. (Functions, Types, Written and Worked Out.)
3.4 Classes
import math
class Point {
x: float
y: float
function distance_to(self, other: Point) -> float {
return math.sqrt((self.x - other.x) ** 2 + (self.y - other.y) ** 2)
}
}
let origin = Point(0.0, 0.0)
let here = Point(x=3.0, y=4.0)
print(origin, here, origin.distance_to(here))Point(x=0.0, y=0.0) Point(x=3.0, y=4.0) 5.0
A class lists its fields, and gets a way to build one for free. It has no parent class: what classes share is said with an interface. (Classes, Interfaces.)
3.5 Enums that carry values, and match
enum Reading {
Value(celsius: float)
Fault(code: int)
Missing
}
function show(r: Reading) -> str {
match r {
case Value(c) { return f"{c:.1f} degrees" }
case Fault(code) { return f"fault {code}" }
case Missing { return "no reading" }
}
}
for r in [Value(21.5), Fault(7), Missing] {
print(show(r))
}21.5 degrees
fault 7
no reading
Leave a case out and Zeno names it before the program
runs. (Enums and match.)
3.6 No surprise none
function find_user(id: int) -> str or none {
if id == 1 {
return "ada"
}
return none
}
let user = find_user(2)
print(user.upper())error: this is str or none. Check it with `is not none` before using `.upper`
--> main.zeno:9:7
|
9 | print(user.upper())
| ^
1 error found. Nothing was run.
A value can be none only if its type says
or none, and then Zeno will not let it be used until the
program has asked:
function find_user(id: int) -> str or none {
if id == 1 {
return "ada"
}
return none
}
for id in [1, 2] {
let user = find_user(id)
if user is none {
print(id, "nobody")
} else {
print(id, user.upper())
}
}1 ADA
2 nobody
3.7 Errors that are checked
function parse_port(text: str) -> int raises ValueError {
let port = int(text)
if not 1 <= port <= 65535 {
raise ValueError(f"{port} is not a port")
}
return port
}
for text in ["8080", "99999", "http"] {
try {
print("port", parse_port(text))
} except ValueError as err {
print("refused:", err)
}
}port 8080
refused: 99999 is not a port
refused: cannot turn "http" into an int
A function says what it raises, and Zeno checks that
every error is either caught or passed on on purpose. (Errors.)
3.8 Functions are values
function counter() {
var count = 0
return function() {
count += 1
return count
}
}
let next_id = counter()
print(next_id(), next_id(), next_id())
print([1, 2, 3].map(function(n: int) -> int { return n * 2 }))1 2 3
[2, 4, 6]
3.9 Dicts
let ages = ["Ada": 36, "Alan": 41]
ages["Grace"] = 85
for name, age in ages {
print(name, age)
}Ada 36
Alan 41
Grace 85
(Dicts.)
3.10 Numbers with units, and numbers with a size
let delay = 1500 ms
print(delay, delay.s, 1 s + 250 ms)
let frame: u8 = 200
print(u8.wrap(frame + 100), frame & 0x0F, frame >> 4)1500 ms 1.5 s 1250 ms
44 8 12
A number can carry a unit, and units that measure the same thing work together. A whole number can have a size, and a number that does not fit is an error unless you ask for it to wrap. (Units, Sized Numbers and Bits.)
3.11 Generics
function largest[T](items: list[T]) -> T {
var best = items[0]
for item in items {
if item > best {
best = item
}
}
return best
}
print(largest([3, 9, 4]), largest(["pear", "fig"]), largest([3.3 V, 5 V]))9 pear 5 V
Written once, checked for each type it is used with, and compiled as if it had been written by hand for that type. (Generics.)
3.12 And on a board
blink.zeno
from board import led
from time import sleep
while true {
led.toggle()
sleep(500 ms)
}$ zeno check blink.zeno
No errors found.
On a computer the board is simulated, and zeno run
prints each change of a pin. The same program builds for firmware with
no garbage collector, no heap and no setjmp. (Pins, Time and the Board, Firmware Builds, Building with No
Collector.)
Take one of the programs in this chapter, break it on purpose, and read the message.
In the enum program, add a fourth case
CalibratingtoReadingand run it before changingshow.In the error program, take
raises ValueErroroffparse_portand runzeno check --stricton the file.