#Streams: fby, next, first
A value can be defined from itself one step back. That is how eLucid8 writes counters, running totals and anything that grows step by step. This lesson builds them up one operator at a time; the Streams reference has every rule.
#A counter
-- A counter: 0, then one more at each step.
n = 0 fby.t n + 1
-- ask n[t = 7]
-- expect n[t = 7] = 7
a fby.t b ("followed by", along t) is a at the first point, t = 0, and b read one step back at every later point (fby). So n is 0, then n + 1 of the step before: 0, 1, 2, 3, …. n has no end: it is a value at every t.
#A running total
-- A running total of the readings.
reading = restrict 20 + mod(#t * 7, 5) to (t : 10) end
total = reading fby.t total + next.t reading
-- ask total[t = 4]
-- expect total[t = 2] = 66
-- expect total[t = 4] = 110
-- expect total[t = 10] = eod
next.t reading is reading one step ahead (next). total starts with the first reading, and each step adds the next one: 20, 42, 66, 87, 110, …. At t = 4 it is 20 + 22 + 24 + 21 + 23 = 110. At t = 10, past the readings, it is eod.
The one thing to watch: everything on the right of fby is read one step back, the coordinate #t included (fby, rule 3). That is why total adds next.t reading: one step back, next gives the reading at the current step.
#The first value
-- The first reading, and each reading's change since it.
reading = restrict 20 + mod(#t * 7, 5) to (t : 10) end
start = first.t reading
change = reading - start
-- ask change[t = 2]
-- expect start = 20
-- expect change[t = 2] = 4
first.t reading is reading at t = 0: 20, with no dimensions (first). change subtracts it at every t.
#Equations, not assignments
These are equations: each says what a value is at every point. total = total + reading says total equals itself plus something, at the same point, which has no answer, and eLucid8 says so before running anything (fby, Mistakes, 1). Write it with fby, as above:
-- An equation that refers to itself at the same point is refused.
reading = restrict 20 + mod(#t * 7, 5) to (t : 10) end
total = total + reading
-- expect compile error: unguarded cycle
#Try it in the playground
- Open the running total: its answer appears by itself,
total[t = 4] is 110. - Press Why?: the trace goes back step by step, from
totalat 4 tototalat 0, reading one reading at each step. - Open the counter and query
natt = 7.nhas no end, so Visualize shows a window of it; drag it, or use the arrows, to move along. - In the running total, write
total = total + reading: it does not compile, so the playground keeps the last version that worked. Press Update to see the error at its line, read it, then press Undo changes.
#Ask your own AI
Using eLucid8's reference at https://elucid8-lang.org/llms.txt, write a Beginner eLucid8 program (equations only: no input or output lines) for a savings account that starts at 100 and adds 5% a year for 30 years, using fby. Compile it with the elucid8 tools before you show it, and give me its Open in playground link.