1 / 26

seven

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)

kanan
Download Presentation

seven

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 • Namemust 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. ► [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 A bad idea

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

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

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

  17. Local names in Meta [with name1= value1name2 = value2 …namen = valuenexpression] • Sort of like pronouns in English • Name objects, but only inside the expression that defines them

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

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

  20. Fine points of scoping [with name1= value1name2 = 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

  21. 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)

More Related