seven name and scope. names in english nouns that refer to specific objects jane spot george w. bush...

Post on 14-Dec-2015

222 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

seven

name and scope

Names in English

Nouns that refer to specific objects Jane Spot George W. Bush Exxon Iceland The Unicorn Café Ben and Jerry’s Zeus

Names in Meta

Denote specific objects Constants (numbers, text strings, …)

1.5 “this is a text string” -7

Variables (words) point box ellipse translate rotate

Defining new names in Meta

[define name value]

Tells Meta that name now refers to value Name must be a single word But value can be an arbitrary expression

Has to be executed to take effect If name already has been defined, redefines it to mean

value

Naming is the most basic abstraction mechanism

A box figure

► [group [box 400 400]

[translate [point 100 100]

[box 70 70]]

[translate [point 100 −100]

[box 70 70]]

[translate [point −100 −100]

[box 70 70]]

[translate [point −100 100]

[box 70 70]]]

Simplifying with names

► [define frame [box 400 400]]

► [define little [box 70 70]]

► [group frame

[translate [point 100 100]

little]

[translate [point 100 −100]

little]

[translate [point −100 −100]

little]

[translate [point −100 100]

little]]

Naming the whole thing

► [define frame [box 400 400]]

► [define little [box 70 70]]

► [define figure [group frame

[translate [point 100 100]

little]

[translate [point 100 −100]

little]

[translate [point −100 −100]

little]

[translate [point −100 100]

little]]

A fancier figure

► [define half-figure [scale 0.5 figure]]

► [define fancy-figure [group frame

[translate [point 100 100]

half-figure]

[translate [point 100 −100]

half-figure]

[translate [point −100 −100]

half-figure]

[translate [point −100 100]

half-figure]]

Going overboard

► [define half-fancy [scale 0.5 fancy-figure]]

► [define really-fancy-figure [group frame

[translate [point 100 100]

half-fancy]

[translate [point 100 −100]

half-fancy]

[translate [point −100 −100]

half-fancy]

[translate [point −100 100]

half-fancy]]

The CS mantra

good software design is enlightened laziness

A bad idea

► [define group [scale 0.5 fancy-figure]]

► [define really-fancy-figure [group frame

[translate [point 100 100]

group]

[translate [point 100 −100]

group]

[translate [point −100 −100]

group]

[translate [point −100 100]

group]]

Error: Attempt to call something that wasn't a procedure

Now you’re in trouble You have no way of making

new groups!

Names just refer to objects Procedures are objects Redefining a name that had

referred to a procedure Doesn’t destroy the procedure But it can make it

inaccessible Because you have no way to

tell Meta you want to use it

Cerebus the Aardvark (Sim, 1977-2004)

The greatest swordsman in the world is an aardvark. The greatest swordsman in the world is Prime Minister. Give the greatest swordsman in the world all of your gold.

Sentences are shorter with names

Cerebus is the greatest swordsman in the world. Cerebus is Prime Minister. Give Cerebus all your gold.

Or later on in the saga …

Most Holy is the greatest swordsman in the world. Most Holy is Pope. Most Holy will make your nose hairs catch fire if you don’t give Most Holy all your gold. Give Most Holy all your gold.

Cerebus don’t talk too good

Cerebus has learned the value of names

However, he has not learned the value of pronouns

This is how we can tell he’s an rotten, smelly, barbarian

Pronouns are your friend

The aardvark drank too much and threw up on the aardvark

The aardvark drank too much and threw up on himself

Expressions that act as names Limited scope

Only meaning for within a single sentence or a few sentences Can be rebound to have other meanings in other sentences

Local names in Meta

[with name1 = value1

name2 = value2

namen = valuen

expression]

Sort of like pronouns in English Name objects, but only inside the expression

that defines them

The fancy figure with local names

► [with frame = [box 400 400] b = [box 70 70] [with g = [group frame [translate [point 100 100] b] [translate [point 100 −100] b] [translate [point −100 −100] b] [translate [point −100 100] b]] [with half = [scale 0.5 figure] [group frame [translate [point 100 100] half] [translate [point 100 −100] half] [translate [point −100 −100] half] [translate [point −100 100] half]]]]]

Local names are your friends

Names are your friends But it’s easy to have two different pieces of code try to use the same

name Group example earlier

Local names Let you control the scope of a name

Reducing the chance of collisions Ensure the definition of the name is near its uses

And so easier to find Allow you to use shorter variable names without getting confused

Less typing Easier to read

Fine points of scoping

[with name1 = value1

name2 = value2 …

namen = valuenexpression]

You can’t use any of the names inside of the values Because they haven’t been defined yet when the values are

computed If there is already a variable with one of the names,

It’s overridden (“shadowed”) by the new name when computing expression

After expression is computed, the old names are reinstated

An intentionally confusing example

[+ 1 [with + = ×

[+ 3 2]]

The names +, −, ×, /, etc. are pre-bound to procedures for addition, subtraction, etc.

But you can always override those by redefine’ing them or by shadowing them with new, local names

So the value of the expression above is 7 I.e. 1+3×2, not 1+(3+2)

top related