#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
How to read it:
mod(a, b)is the remainder ofadivided byb.#t * 7is 0, 7, 14, 21, …, and its remainder by 5 is 0, 2, 4, 1, 3, 0, 2, …. Soreadingis 20, 22, 24, 21, 23, 20, 22, 24, 21, 23: ten readings that go up and down without being a straight line. The next lessons use them.sinpi(z)is the sine of π timesz. Overh(hours),temprises from 15 to 20 at hour 6, falls to 10 at hour 18, and is back at 15 at hour 24: a daily cycle, twice.
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
- The answer is there as the program opens, with no button to press:
reading[t = 2] is 24. - In Visualize, look at the row of readings and its chart. Ask for
tempath = 6and look at its two days. - Change the 7 in
readingto 3: the picture redraws by itself, with other readings. The version line says what changed. - 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.