61A Lecture 28Friday, November 4Friday, November 4, 2011The Logo Programming LanguageA teaching language: designed for introductory programmingOne syntactic form for all purposes: invoking a procedureOnly two data types: words and sentencesCode is data: a line of code is a sentenceAn elegant tagline: no threshold, no ceilingA bit of fun: turtle graphics2DemoFriday, November 4, 2011http://imgs.xkcd.com/comics/lisp_cycles.pngLogo is a Dialect of LispWhat are people saying about Lisp?•"The greatest single programming language ever designed." -Alan Kay (from the UI video), co-inventor of Smalltalk•"The only computer language that is beautiful." -Neal Stephenson, John's favorite sci-fi author•"God's programming language." -Brian Harvey, Father of CS 61A3Friday, November 4, 2011Logo FundamentalsCall expressions are delimited by spacesLogo procedures are equivalent to Python functions•A procedure takes inputs (arguments) that are values•A procedure returns an output (return value)•A procedure may output None to indicate no return value4? print 55Multiple expressions can appear in a single line? print 1 print 212Friday, November 4, 2011Nested Call ExpressionsThe syntactic structure of expressions is determined by the number of arguments required by named procedures5? print sum 10 difference 7 314print takes one argument (input)sum takes two inputsdifference takes two inputs tooOne nested call expressionversusTwo expressions on one line? print 1 print 212DemoFriday, November 4, 2011Data Types and QuotationWords are strings without spaces, representing text, numbers, and boolean values6? print "hellohello? print "sumsum? print "22Sentences are immutable sequences of words and sentences? print [hello world]hello world? show [hello world][hello world]Friday, November 4, 2011Sentence (List) Processing in LogoSentences can be constructed from words or sentences7Procedure Effectsentence Output a sentence containing all elements of two sentences. Input words are converted to sentences.list Output a sentence containing the two inputs.fput Output a sentence containing the first input and all elements in the second input.DemoFriday, November 4, 2011Expressions are SentencesThe run procedure evaluates a sentence as a line of Logo code and outputs its value8? run [print sum 1 2]3? run sentence "print [sum 1 2]3? print run sentence "sum sentence 10 run [difference 7 3]14Its argument can be constructed from other procedure calls4[10 4]14Friday, November 4, 2011ProceduresProcedure definition is a special form, not a call expression9? to double :x> output sum :x :x> end? print double 48Procedure name Formal parameterBodyProcedures are not first-class objects in Logo; they can only ever be referenced by their original procedure nameProcedure names can be inputs or outputsFriday, November 4, 2011Conditional ProceduresIf and ifelse are regular procedures in LogoMeaning: They do not have a special evaluation procedureThey take sentences as inputs and run them conditionally10? to reciprocal :x> if not :x = 0 [output 1 / :x]> output "infinity> end? print reciprocal 20.5? print reciprocal 0infinityFriday, November 4, 2011Dynamic ScopeWhen one function calls another, the names bound in the local frame for the first are accessible to the body of the secondNo isolation of formal parameters to function bodies, as we saw with lexical scope11? to print_x :x> print_last_x> end? to print_last_x> print :x> end? print_x 55Friday, November 4, 2011Logo ExamplesDemo12Friday, November 4, 2011Decoding a sequence of bits:Homework: Huffman Encoding TreesEfficient encoding of strings as ones and zeros (bits).13 A 0 C 1010 E 1100 G 1110 B 100 D 1011 F 1101 H 1111ABC DE F G H0 10 10 10 10 10 1 0 11 0 0 0 1 0 1 0B A CFriday, November 4,
View Full Document