Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


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:

1 seven name and scope

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

3 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

4 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

5 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]]]

6 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]]

7 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]]

8 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]]

9 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]]

10 The CS mantra good software design is enlightened laziness

11 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

12 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.

13 Sentences are shorter with names Cerebus is the greatest swordsman in the world. Cerebus is Prime Minister. Give Cerebus all your gold.

14

15

16 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.

17

18

19

20 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

21 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

22 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

23 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]]]]]

24 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

25 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

26 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)


Download ppt "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."

Similar presentations


Ads by Google