Learn › Beginner · Lesson 6 of 11

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

Open in playground

How to read it:

Try it in the playground

  1. The answer is there as the program opens, with no button to press: capped[t = 2] is 22.
  2. Query warm and look at Visualize: true and false in two colours along the row.
  3. Query safe at t = 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.
  4. 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.