L4K ︰小海龜繪圖《VIII》

Turtle Art 程式指南文本之作者用『鞋盒』

A SHOEBOX

When explaining boxes in workshops, I often use a shoebox. I have someone write a number on a piece of paper and put it in the shoebox. I then ask repeatedly, “What is the number in the box?” Once it is clear that we can reference the number in the shoebox, I have someone put a different number in the shoebox. Again I ask, “What is the number in the box?” The power of the box is that you can refer to it multiple times from multiple places in your program.

 

的想法解釋小海龜繪圖裡的『箱子』積木

2. Boxes

Boxes let you store an object, e.g., a number, and then refer to the object by using the name of the box. (Whenever you name a box, a new block is created on the Boxes palette that lets you access the content of the box.) This is used in a trivial way in the first example below: putting 100 in the box and then referencing the box from the Forward block. In the second example, we increase the value of the number stored in the box so each time the box is referenced by the Forward block, the value is larger.

Putting a value in a Box and then referring to the value in Box

We can change the value in a Box as the program runs. Here we add 10 to the value in the *Box( with each iteration. The result in this case is a spiral, since the turtle goes forward further with each step.

If we want to make a more complex change, we can store in the Box some computed value based on the current content of the Box. Here we multiply the content of the box by 1.2 and store the result in the Box. The result in this case is also a spiral, but one that grows geometrically instead of arithmetically.

In practice, the use of boxes is not unlike the use of keyword-value pairs in text-based programming languages. The keyword is the name of the Box and the value associated with the keyword is the value stored in the Box. You can have as many boxes as you’d like (until you run out of memory) and treat the boxes as if they were a dictionary. Note that the boxes are global, meaning all turtles and all action stacks share the same collection of boxes.

 

這個看似簡單直覺的概念

一個命了名的『箱子』,裡面裝著粅件。

用以表達『名稱‧賦值』這件事情。

若是問起『未賦值』的『空箱子』裡有什麼呢?

【命名為 my box 的箱子】

turtle-art-box-named-variable-numeric-value

 

【box 1 箱子積木】

turtle-art-box-1-variable-1-numeric-value

 

【命名為 box 1 的箱子】

turtle-art-box-1-variable-1-numeric-value_1

 

有點令人驚訝吧!語言之理解務求精確乎☆

 

turtle-art-flow-palette-of-flow-operators