2. Values
Goal
Show a sentence the program computes.
New in this chapter
(apples times price)form paren spec/02 §2.3{"Total: ", …}form list spec/04 §4.43,5.5form value-number spec/01 §1.5set total to …form set-local spec/02 §2.3a plus bform plus spec/04 §4.5a minus bform minus spec/04 §4.5a times bform times spec/04 §4.5a divided by bform divided-by spec/04 §4.53type number spec/04 §4.1list of texttype list spec/04 §4.2text.Joinpackage std/text std/README.md § std/text
Steps
We start from the last program of chapter 1. A local is a name for a value: set price to 4 creates price. Showing a number needs a conversion, because a caption is text; text.From turns any value into text. A phrase with a value inside another sentence goes in parentheses:
Step 1
# package main
use std/lesson
use std/text
# Say hello, then work out the bill.
define main
set price to 4
lesson.Note "Hello, Synod"
lesson.Note (text.From price)
Locals can be computed from other locals. times is one of four arithmetic words, written between its two values:
Step 2
# package main
use std/lesson
use std/text
# Say hello, then work out the bill.
define main
set apples to 3
set price to 4
set total to (apples times price)
lesson.Note "Hello, Synod"
lesson.Note (text.From total)
Showing "13" alone is not much of a sentence. text.Join glues a list of texts together. A list is written in braces, items separated by commas, and each item is one thing: a text, or a parenthesized sentence such as (text.From total):
Step 3
# package main
use std/lesson
use std/text
# Say hello, then work out the bill.
define main
set apples to 3
set price to 4
set total to (apples times price)
lesson.Note "Hello, Synod"
lesson.Note (text.Join {"Total: ", (text.From total)})
Now the whole bill. plus, minus and divided by work like times. An operator takes exactly two items, so a longer calculation nests parentheses, and there are no precedence rules to learn:
Step 4
# package main
use std/lesson
use std/text
# Say hello, then work out the bill.
define main
set apples to 3
set price to 4
set tip to 1
set coupon to 2
set people to 2
set total to ((apples times price) plus tip)
set share to ((total minus coupon) divided by people)
lesson.Note "Hello, Synod"
lesson.Note (text.Join {"Total: ", (text.From total)})
lesson.Note (text.Join {"Each pays ", (text.From share)})
What to watch
Three captions in order: the greeting, Total: 13 and Each pays 5.5. Everything happened at the very first instant; the computation is not drawn, only its result.
Why it looks like this
A local that nobody reads is almost always a mistake, so Synod refuses it. Here coupon is set and never used:
hello/hello.synod:9:7: error[unused-name]: "coupon" is never used
|
9 | set coupon to 2
| ^^^^^^
unused-nameThe diagnostic is unused-name. Use the value, or delete the line (spec/02 §2.3).
Exercises
- Change
tipto 3. What does each person pay?
Expected
The total is 15 and each pays 6.5.
- Write
(apples times price plus tip)and runsynod check hello. What happens?
Expected
It is rejected: an operator takes two items, so you must write ((apples times price) plus tip).
- Add
lesson.Note (text.Join {"People: ", (text.From people)}).
Expected
A fourth caption, People: 2.