Learn › Beginner · Lesson 3 of 11

Data written as formulas

Real programs read data: readings, prices, temperatures. In these lessons the data are written in the program itself, as equations, so the whole program is one piece of text you can read, change and share. (Reading your own files comes in the Intermediate lessons.)

A formula over a coordinate can make numbers that look like data:

-- Made-up data: ten readings, and two days of a temperature cycle.
reading = restrict 20 + mod(#t * 7, 5) to (t : 10) end
temp = restrict 15 + 5 * sinpi(#h / 12) to (h : 48) end
-- ask reading[t = 2]
-- expect reading[t = 0] = 20
-- expect reading[t = 2] = 24
-- expect reading[t = 3] = 21
-- expect temp[h = 6] = 20
-- expect temp[h = 18] = 10

Open in playground

How to read it:

Other ways to make data: if to pick values (Lesson 6), #t * #t for growth, mod(#x + #y, 2) for a pattern on a grid.

Try it in the playground

  1. The answer is there as the program opens, with no button to press: reading[t = 2] is 24.
  2. In Visualize, look at the row of readings and its chart. Ask for temp at h = 6 and look at its two days.
  3. Change the 7 in reading to 3: the picture redraws by itself, with other readings. The version line says what changed.
  4. Press Undo to go back to the readings above.

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) that gives made-up hourly temperatures for a week as a formula, with a warm afternoon each day. Compile it with the elucid8 tools before you show it, and give me its Open in playground link.