Chapter 12 Em

download Chapter 12 Em

of 9

Transcript of Chapter 12 Em

  • 7/31/2019 Chapter 12 Em

    1/9

  • 7/31/2019 Chapter 12 Em

    2/9

    Variables: Big Squares and SmallSquares

    You might want the turtle to draw squares withsides of 60,50,100,10 and so on. One way of doingthis is to have many procedures: s q u a r e 100,

    s q u a r e 50, s q u a r e 33, etc. But there must be abetter way, some kind of shortcut; and, of course,there is. We can change s q u a r e s o that it is likef o r w a r d in that it takes an input. Then we cantell s q u a r e how long to make its sides by typing

    SQUARE 50

    SQUARE 40SQUARE 30

    So lets make a procedure for drawing variablesized squares, b o x might be a suitable name sinceit reminds us of squares. But well meet a newidea by calling it b o x r (usually pronounced likebox-are) which makes us think of a right-turningsquare. We could also define a left-turning boxand call it b o x l (usually pronounced box-ell).A shortcut method for typing in the definition ofb o x r is to modify s q u a r e in the editor. If wechange the name of the procedure before we leavethe editor we will not change the definition ofSQUARE.

    EDIT " SQUARE

    Now the Logo editor shows this procedure withthe! (cursor) on the t oft o .

    TO SQUARE

    FD 30

    RT 90

    FD 30RT 90

    FD 30

    RT 90

    FD 30

    RT 90

    END

    81

  • 7/31/2019 Chapter 12 Em

    3/9

    First lets change the name of the procedure froms q u a r e to b o x r . The I (cursor) is sitting on the t of t o . Move it to the space after s q u a r e . Usec t r l -e .

    TO SQUARE

    Now erase the word s q u a r e using the - key andtype the word b o x r

    TO BOXR

    Type c t r l - c . Logo respondsBOXR DEFINED

    At this point b o x r and s q u a r e have the samedefinition. Now we change b o x r Type

    E D I T " B O X R

    The Logo editor now shows

    TO BOXR

    FD 30

    RT 90

    FD 30

    RT 90

    FD 30

    RT 90FD 30

    RT 90

    END

    We change b o x r so that it requires an input likef o r w a r d does. The procedure will be able todraw squares of various sizes. How do we tellLogo to do this?

    The first instruction has to be f o r w a r d someamount, but when we are writing the procedurewe dont know what amount it will be. We handlethis situation by giving the amount a name. Forexample, lets call it s i d e . If we choose this name,

    821

  • 7/31/2019 Chapter 12 Em

    4/9

    we write f d :s i d e to mean f d whatever numberhappens to be called s i d e . So we can write

    FDRT

    FD

    RT

    FD

    RT

    FD

    RT

    : SIDE90

    : SI DE90

    : SI DE90

    : SI DE90

    One more idea is needed to turn this into a proce

    dure. When we use the command b o x r we willnow have to follow it with an input like this:

    BOXR

    BOXR

    20100

    To indicate that b o x r needs an input and thatthis input will be called s i d e we do the whole pro

    cedure like this:

    TO BOXR : SIDE FD : SIDE RT 90

    FD : SI DERT 90

    FD : SI DERT 90

    FD : SIDE RT 90

    END

    b o x r makes the turtle draw a square of any sizedepending upon the number you give it as aninput.

    BOXR 10

    BOXR 20

    BOXR 30

    BOXR 40 83

  • 7/31/2019 Chapter 12 Em

    5/9

    We have just used a powerful mathematical idea the idea of variable. But instead of using a mys

    terious x for the variable as we did in school alge

    bra, we have used a meaningful name, s i d e .

    When you were defining this procedure you wanted to tell the turtle how to draw a square, butyou did not know what size square you mightneed. Indeed, you wanted to be able to drawsquares of all possible sizes. When you came to typing the f o r w a r d command you knew thatf o r w a r d needed an input. You couldnt just typef o r w a r d without an input. You had to type f o r

    w a r d something. To give this something a name,we called it s i d e . In Logo the expression :s i d e means whatever happens to be in the containercalled s i d e . If Logo is to carry out the commandf o r w a r d :s i d e there must be something in thecontainer.

    The container is filled when you use b o x r andtype b o x r 10 or b o x r 15. When Logo obeys thatcommand, 10, 15, or whatever you typed as the input is put in the container named s i d e , b o x r can then look in the container at a later time.

    Bug BoxPossible bugs:

    1. You typed :SISE or some other spelling differ

    ent from the way the input on the title line wetsspelled.

    2. You forgot to use dots (:).

    3. You inserted an extra instruction in b o x r .

    4. You accidentally erased an instruction inBOXR.

    5. You typed a space between : and s i d e .

    6. You typed a : in front of a number.

    --

    ----------------------------------------

    84!

  • 7/31/2019 Chapter 12 Em

    6/9

    The character, : (dots), informs Logo that theword to which it is prefixed names a containerwhich can have in it a number, another word, a listof words, or a list of lists.

    Some Uses ofb o x r

    TO SQUARES

    BOXR 10

    BOXR 20

    BOXR 30

    BOXR 40

    END

    TO DIAMONDSRT 45REPEAT 4 [SQUARES RT 90]

    END

    TO FLAGR : S I ZE

    FD : S I ZE

    BOXR : S I ZE

    BK : S I Z E

    END

    TO 6FLAG : S I ZEREPE AT 6 [FL AGR : S I Z E RT 60]

    END

    TO S P I N F L A G : S I Z E

    6 F L A G : S I Z E

    6 F LA G : S I Z E - 20

    END

    Being able to control the size of a shape makesthat procedure much more useful and interesting.

  • 7/31/2019 Chapter 12 Em

    7/9

    I k 3 8 R T 1 2 0 3

    Big Triangles and Small Triangles

    We can also define a triangle procedure which

    takes an input. Type

    ED "TRIANGLE

    Now the Logo editor shows this procedure with

    the I (cursor) on the t oft o .

    TO TRIANGLE

    RE PE AT 3 [FD 30 RT 120]

    END

    Then change t r i a n g l e .

    TO T R I A N G L E R : S I D E

    REPE AT 3 [ F D : S ID E RT 120 ]

    END

    Use the procedure to make designs like:

    TRIANGLES TRISTAR TREES

    TRIANGLES

    T O T R I A N G L E S

    T R I A N G L E R 1 0

    T R I A N G L E R 2 0T R I A N G L E R 3 0

    T R I A N G L E R 4 0

    END

    TO TR ISTAR

    R E P E A T 1 0 [ T R I A N G L E S RT 3 6 ]

    END

    TO TREE : S ID ERT 3 0 T R I A N G L E R : S I D E

    RT 60 FD : S I DE / 2

    LT 90 BK : S I DE / 2

    END

    TREES 86

  • 7/31/2019 Chapter 12 Em

    8/9

    TO TREESTREE 30

    TREE 40

    TREE 50

    END

    Arithmetic

    As you have learned from the examples above,you can do arithmetic in Logo. For example,

    PR 5 + 3

    Logo types8

    PR 4 * 23

    Logo types92

    PR 345 - 32

    Logo types3 13

    PR 25 / 5

    Logo types

    5 .

    Logo Numbers

    Notice that the result of 25 divided by 5 is a deci

    mal number. Logo has both decimal and integernumbers. Logo will perform arithmetic with deci

    mals or integers.

  • 7/31/2019 Chapter 12 Em

    9/9

    Some computations always result in a decimal an

    swer. Division (/) is one of those. Other arithmeticoperations depend on what they are given as inputs. For example, if you type

    PR 4 * 2 . 3

    Logo types9 . 2

    For more discussion about arithmetic in Logoconsult theReference Manual.

    Logo Vocabulary

    The character: (dots) informs Logo that the wordto which it is prefixed names a container whichcan contain a number, another word, a list ofwords, or a list of lists.

    Arithmetic Operations

    +*

    88