#Choices: if, comparisons, and, or, not
if c then a else b gives a where c is true and b where it is false, point by point. Comparisons (<, <=, >, >=, ==, !=) give true or false; and, or and not combine them.
-- Choices on the readings.
reading = restrict 20 + mod(#t * 7, 5) to (t : 10) end
warm = reading > 22
capped = if reading > 22 then 22 else reading
steady = reading >= 21 and reading <= 23
safe = restrict if #t == 0 then 0 else 100 / #t to (t : 10) end
-- ask capped[t = 2]
-- expect warm[t = 2] = true
-- expect warm[t = 3] = false
-- expect capped[t = 2] = 22
-- expect capped[t = 3] = 21
-- expect steady[t = 0] = false
-- expect steady[t = 4] = true
-- expect safe[t = 0] = 0
-- expect safe[t = 4] = 25
How to read it:
warmis true where the reading is above 22: att = 2(24), not att = 3(21).cappedkeeps a reading at most 22: 24 becomes 22, 21 stays 21.steadyis true where the reading is between 21 and 23.safedivides 100 by#t, except att = 0. Only the branch chosen is computed, so att = 0the division by zero never happens: theifis a guard.
#Try it in the playground
- The answer is there as the program opens, with no button to press:
capped[t = 2] is 22. - Query
warmand look at Visualize: true and false in two colours along the row. - Query
safeatt = 0: 0. Now remove the guard,safe = restrict 100 / #t to (t : 10) end: the answer is asked again by itself, and it is an error, division by zero. Undo puts the guard back. - Press Why? on
capped[t = 2]: the trace shows the condition, then only the branch it chose.
#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 traffic light that is green for 5 steps, amber for 1 and red for 4, over and over. Compile it with the elucid8 tools before you show it, and give me its Open in playground link.