'&$%CSE 341:Programming LanguagesDan GrossmanSpring 2004Lecture 6— (The truth about bindings) and course motivationDan Grossman CSE341 Spring 2004, Lecture 6 1'&$%Where we areClass has covered tremendous ground—you should catch up by doinghomework 2.Next week we’ll take up first-class functions (closures, functions asvalues):• A really key idea in computer scienceBut we haven’t yet seen that pattern-matching is an elegantgeneralization of variable binding.And I owe you an explanation of why we should study programminglanguages, particulary ML, Scheme, and SmalltalkDan Grossman CSE341 Spring 2004, Lecture 6 2'&$%Deep patternsPatterns are much richer than we have let on. A pattern can be:• A variable (matches everything, introduces a binding)• _ (matches everything, no binding)• A constructor and a pattern (e.g., C p) (matches a value if thevalue “is a C” and p matches the value it carries)• A pair of patterns ((p1,p2)) (matches a pair if p1 matches thefirst component and p2 matches the second component)• A record pattern...• The constant () (matches the value ()• An integer constant...• ...Dan Grossman CSE341 Spring 2004, Lecture 6 3'&$%Can you handle the truth?It’s really:• val p = e• fun f p1 = e1 | f p2 = e2 ... | f pn = en• case e of p1 => e1 | ... | pn => enInexhaustive matches may raise exceptions and are bad style.Example: could write Rope pr or Rope (r1,r2)Fact: Every ML function takes exactly one argument!Dan Grossman CSE341 Spring 2004, Lecture 6 4'&$%Some function examples• fun f1 () = 34• fun f2 (x,y) = x + y• fun f3 pr = let val (x,y) = pr in x + y endIs there any difference to callers between f2 and f3?In most languages, “argument lists” are syntactically separate,second-class constructs.Dan Grossman CSE341 Spring 2004, Lecture 6 5'&$%A question?What’s the best car?What are the best kind of shoes?Dan Grossman CSE341 Spring 2004, Lecture 6 6'&$%Aren’t all languages the same?Yes: Any input-output behavior you can program in language X youcan program in language Y• Java, ML, and a language with one loop and three infinitely-largeintegers are “equal”• This is called the “Turing tarpit”Yes: Certain fundamentals appear in most languages (variables,abstraction, each-of types, inductive definitions, ...)• Travel to learn more about where you’re fromNo: Most cars have 4 tires, 2 headlights, ...• Mechanics learn general principles and what’s differentDan Grossman CSE341 Spring 2004, Lecture 6 7'&$%Aren’t these academic languages worthless?In the short-term, maybe: Not many summer internships using ML?But:• Knowing them makes you a better Java, C, and Perl programmers(affects your idioms)• Java did not exist in 1993; what does not exist now?• Do Java and Scheme have anything in common? (Hint: check theauthors)• Eventual vindication: the garbage-collection exampleDan Grossman CSE341 Spring 2004, Lecture 6 8'&$%Aren’t the semantics my least concern?Admittedly, there are many important considerations:• What libraries are available?• What does my boss tell me to do?• What is the de facto industry standard?• What do I already know?Technology leaders affect the answers to these questions.Sound reasoning about programs, interfaces, and compilers requiresknowledge of semantics.Dan Grossman CSE341 Spring 2004, Lecture 6 9'&$%Aren’t languages somebody el se’s problem?If you design an extensible software system, you’ll end up designing a(small?) programming language!Examples: VBScript, JavaScript, PHP, ASP, QuakeC, Renderman,bash, AppleScript, emacs, Eclipse, AutoCAD, ...Another view: A language is an interface with just a few functions(evaluate, typecheck) and a sophisticated input type.In other words, an interface is just a stupid programming language.Dan Grossman CSE341 Spring 2004, Lecture 6 10'&$%SummaryThere is no such thing as a “best programming language”. (There aregood general design principles we will study.)A good language is a relevant, crisp, and clear interface for writingsoftware.Software leaders should know about programming languages.Learning languages has super-linear payoff.• But you have to learn the semantics and idioms, not a cutesyntactic trick for printing “Hello World”.End of the course: Language-design goals, mechanisms, and trade-offsNext time: why ML, Scheme, and Smalltalk?Dan Grossman CSE341 Spring 2004, Lecture 6
View Full Document