← Sprout

Chapter 1 · A Seed in Your Hand

A Slightly Soggy Guide to Sprout

In which two seedlings learn to talk to a computer, a watering can complains about nearly everything, and you grow a small plant out of about twenty lines of code.

Pipnine days old, with an opinion about each of them
Budlives in the next pot; would like to know why we're up
Mrs. Spouta watering can of great age and greater patience, now running low

You are holding a seed

A seed doesn't look like much. Neither does Sprout. That's on purpose: almost everything in it is one move, done over and over. You write a value, then a word, and the word goes to the value, and the value answers.

"sprout" length
-- 6
"sprout" upper
-- SPROUT
[3, 1, 2] sort
-- [1, 2, 3]

The grey lines that start with -- are the answers. Every answer in this chapter was worked out by Sprout itself, just before this page was made, so if one of them is wrong it's Sprout's fault and not mine, and Sprout is very sorry.

Mrs. Spout! What's one plus two times three?

Seven. Everybody knows that. Can I go back to sleep?

Nine. Sprout reads the way you do: left to right. One plus two is three, times three is nine.

1 + 2 * 3
-- 9
1 + (2 * 3)
-- 7

If you want something done first, put it in round brackets, and Sprout does that bit before it carries on. There's no table of which sign beats which to remember. Mrs. Spout says this is the only sensible thing about computers, and she has seen a great many of them, mostly from the bottom of a shed.

A short digression about wordsWords in Sprout can have dashes in them: pad-left, starts-with, pressed if you're feeling plain. Mrs. Spout once knew a word with a dash in it. It was over-watered. She doesn't talk about it, and neither should you.

A cell is a little pot

When you want to do the same thing again later, you put the doing in a cell. A cell is square brackets around some code, with the names of what goes in before a bar.

double : [x | x * 2]
double 21
-- 42
grow : [height days | height + (days * 3)]
grow [4, 5]
-- 19

Give a cell one thing and it takes it plainly. Give it several in a list and they line up with its names in order: four for height, five for days. A cell is just a value, too, so you can name it, hand it to another cell, keep it in a list, or leave it on a windowsill to think about what it's done.

So a cell is code in a pot?

And you can carry the pot anywhere!

You can. Don't drop it.

Records, lists, and nothing ever changes

A list is things in square brackets. A record is the same, but every thing has a name. Pip, for one, is a record.

pip : [name : "Pip", leaves : 2, mood : "sunny"]
pip leaves
-- 2
pip with [leaves : 3]
-- [name : Pip, leaves : 3, mood : sunny]
pip leaves
-- 2

Look at that last answer. Pip still has two leaves. with didn't change Pip; it grew a new record with three leaves and left the old one exactly as it was. In Sprout nothing you've made ever changes behind your back, which is restful, like a pot that never tips over.

Did with hurt Pip?

Nothing in Sprout ever changes, dear. You get a new one, and the old one is just as it was.

There are two of me now, and one of us has three leaves!

A whole garden at once

Most of the time you don't have one thing, you have a bed full of them. collect does something to every one and gives you the new ones back; keep keeps the ones you say yes to.

garden : [3, 8, 1, 12, 5]
garden collect [h | h + 1]
-- [4, 9, 2, 13, 6]
garden keep [h | h > 4]
-- [8, 12, 5]
garden collect [h | h + 1] keep [h | h > 4] length
-- 3

Read the last line left to right, the way Sprout does: every plant grows by one, the tall ones are kept, and the count of them is three. Each word hands its answer to the next, like a row of gardeners passing a pot down a line.

beds : [[name : "Pip", leaves : 2], [name : "Bud", leaves : 1], [name : "Fern", leaves : 5]]
beds keep [p | p leaves > 1] collect [p | p name]
-- [Pip, Fern]
Mrs. Spout's grumbles, numbered1. collect gives back as many as it was given. Always. 2. keep gives back fewer, or the same, and never more. 3. Neither of them waters anything. That's still my job.

When Sprout doesn't understand

Sooner or later you'll ask a value for something it hasn't got. Sprout doesn't stop and sulk. It says what it didn't understand and on which line, makes a guess at what you meant when it has one, and carries on with false in place of the answer.

pip leafs
-- [name : Pip, leaves : 2, mood... doesn't know leafs (did you mean leaves?)
[3, 8, 1] max
-- [3, 8, 1] doesn't know max (did you mean largest?)
"Pip has " + 2
-- "Pip has " doesn't know + (to join text, use .., as in "score " .. 10)

Read the message from the left: first the value, then the word it was sent. The value is nearly always the clue. If it says false, something on an earlier line missed first, and that's the one to fix. Mrs. Spout reads them aloud in a tone of quiet disappointment, which helps nobody, but she says it's tradition.

A game you can tap

A Sprout game is three names: start, how things are when it begins; update, what happens next, given how things are now; and draw, what to show. Sprout runs update sixty times a second and hands it the taps. Here is a whole game, and it is alive.

Tap the picture to water. Keep watering and it blooms; leave it, and it droops a little, as plants and people do.

start : [height : 12, rain : 0]

update : [game |
  g : game
  wet : (g input tapped) [1] else [g rain - (g dt * 2) max 0]
  growth : (wet > 0) [g dt * 40] else [g dt * -1]
  g with [rain : wet, height : g height + growth max 12 min 130]
]

draw : [game |
  top : 170 - (game height)
  sway : (game clock * 2) sin * 3
  clear #fdf3dc
  rect [0, 170, 300, 30, #8b5a2b]
  rect [148, top, 4, game height, #3f8f2c]
  circle [140 + sway, top, 10, #6cc04a]
  circle [160 + sway, top, 10, #6cc04a]
  game rain > 0
    range 6 each [i | circle [100 + (i * 20), 1 - (game rain) * 140 + 20 + (i * 7 % 13), 3, #4aa8e0]]
  game height >= 130
    circle [150, top - 9, 7, #f49aa0]
  text [(game height >= 130) ["IN BLOOM"] else ["TAP TO WATER"], 150, 186, 12, #fdf3dc]
]

Notice update never touches the old game. It makes a new one with a little more rain and a little more height, and Sprout keeps the new one for the next tick. (In Node, just now, this plant was 12 tall, then 13.3 a moment after a tap, with 12 shapes drawn.)

The indented lines under game rain > 0 are drawn only while it's raining: a line on its own followed by indented lines means "only when". That's all the grammar in this game, and very nearly all the grammar there is.

The end of chapter one

Pip is asleep. Bud has been asleep since the section on records. Mrs. Spout says that next time we'll make things move, find out what a cell can remember, and that she'll be there, because she is always there.