Presentation on theme: "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."— Presentation 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
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 ] group] [translate [point 100 −100] group] [translate [point −100 −100] group] [translate [point − ] 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, ) 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 name 1 = value 1 name 2 = value 2 … name n = value n expression] Sort of like pronouns in English Name objects, but only inside the expression that defines them
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 name 1 = value 1 name 2 = value 2 … name n = value n expression] 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)