Unformatted text preview:

99CS 538 Spring 2006©Reading Assignment• Roosta: Sections 13.1, 13.2• The Scheme Language Definition (linked from class web page)100CS 538 Spring 2006©Lisp & SchemeLisp (List Processing Language) is oneof the oldest programming languagesstill in wide use.It was developed in the late 50s andearly 60s by John McCarthy.Its innovations include:• Support of symbolic computations.• A functional programming stylewithout emphasis on assignmentsand side-effects.• A naturally recursive programmingstyle.• Dynamic (run-time) type checking.• Dynamic data structures (lists, binarytrees) that grow without limit.101CS 538 Spring 2006©• Automatic garbage collection tomanage memory.• Functions are treated as “first class”values; they may be passed asarguments, returned as result values,stored in data structures, and createdduring execution.• A formal semantics (written in Lisp)that defines the meaning of all validprograms.• An Integrated ProgrammingEnvironment to create, edit and testLisp programs.102CS 538 Spring 2006©SchemeScheme is a recent dialect of Lisp.It uses lexical (static) scoping.It supports true first-class functions.It provides program-level access tocontrol flow via continuationfunctions.103CS 538 Spring 2006©Atomic (Primitive) Data TypesSymbols:Essentially the same form asidentifiers. Similar to enumerationvalues in C and C++.Very flexible in structure; essentiallyany sequence of printable charactersis allowed; anything that starts avalid number (except + or -) may notstart a symbol.Valid symbols include: abc hello-world + <=!Integers:Any sequence of digits, optionallyprefixed with a + or -. Usuallyunlimited in length.104CS 538 Spring 2006©Reals:A floating point number in a decimalformat (123.456) or in exponentialformat (1.23e45). A leading sign anda signed exponent are allowed(-12.3, 10.0e-20).Rationals:Rational numbers of the form integer/integer (e.g.,1/3 or 9/7) with anoptional leading sign (-1/2, +7/8).Complex:Complex numbers of the formnum+num i or num-num i, wherenum is an integer or real number.Example include 1+3i, -1.5-2.5i,0+1i).105CS 538 Spring 2006©String:A sequence of characters delimited bydouble quotes. Double quotes andbackslashes must be escaped using abackslash. For example"Hello World" "\"Wow!\""Character:A single character prefixed by#\. Forexample,#\a, #\0, #\\, #\#. Twospecial characters are#\space and#\newline.Boolean:True is represented as#t and false isrepresented as#f.106CS 538 Spring 2006©Binary TreesBinary trees are also calledS-Expressions in Lisp and Scheme.They are of the form ( item . item )where item is any atomic value or anyS-Expression. For example: ( A . B ) (1.2 . "xyz" ) ( (A . B ) . C ) ( A . (B . C ) )S-Expressions are linearizations ofbinary trees:AB1.2"xyz"107CS 538 Spring 2006©S-Expressions are built and accessedusing the predefined functions cons,car and cdr.cons builds a new S-Expression fromtwo S-Expressions that represent theleft and right children.cons(E1,E2) = (E1 . E2)car returns are left subtree of anS-Expression.car (E1 . E2) = E1cdr returns are right subtree of anS-Expression.cdr (E1 . E2) = E2CAABBC108CS 538 Spring 2006©ListsIn Lisp and Scheme lists are a special,widely-used form of S-Expressions.() represents the empty or null list(A) represents the list containing A.By definition,(A) ≡ (A . () )(A B) represents the list containing Aand B. By definition,(A B) ≡ (A . (B . () ) )In general, (A B C ... Z) ≡(A . (B . (C . ... (Z . () ) ... )))(A B C )≡ABC()109CS 538 Spring 2006©Function CallsIn List and Scheme, function calls arerepresented as lists.(A B C) means:EvaluateA (to a function)EvaluateB and C (as parameters)CallA with B and C as its parametersThen use the value returned by thecall as the “meaning” of(A B C).cons, car and cdr are predefinedsymbols bound to built-in functionsthat build and access lists andS-Expressions.Literals (of type integer, real, rational,complex, string, character andboolean) evaluate to themselves.110CS 538 Spring 2006©For example (⇒ means “evaluatesto”)(cons 1 2) ⇒ (1 . 2)(cons 1 () ) ⇒ (1)(car (cons 1 2)) ⇒ 1(cdr (cons 1 ())) ⇒ ()But,(car (1 2)) fails during execution!Why?The expression(1 2) looks like a call,but1 isn’t a function! We need someway to “quote” symbols and lists wedon’t want evaluated.(quote arg)is a special function that returns itsargument unevaluated.111CS 538 Spring 2006©Thus (quote (1 2)) doesn’t try toevaluate the list(1 2); it just returnsit.Since quotation is so often used, itmay be abbreviated using a singlequote. That is(quote arg) ≡ 'argThus(car '(a b c)) ⇒ a(cdr '( (A) (B) (C))) ⇒( (B) (C) )(cons 'a '1) ⇒ (a . 1)But,('cdr '(A B)) fails!Why?112CS 538 Spring 2006©User-defined FunctionsThe list(lambda (args) (body))evaluates to a function with (args)as its argument list and (body) asthe function body.No quotes are needed for(args) or(body).Thus(lambda (x) (+ x 1)) evaluatesto the increment function.Similarly,((lambda (x) (+ x 1)) 10) ⇒11113CS 538 Spring 2006©We can bind values and functions toglobal symbols using thedefinefunction.The general form is(define id object)id is not evaluated but object is. idis bound to the value object evaluatesto.For example,(define pi 3.1415926535)(define plus1 (lambda (x) (+ x 1)) )(define pi*2 (* pi 2))Once a symbol is defined, it evaluatesto the value it is bound to:(plus1 12) ⇒ 13114CS 538 Spring 2006©Since functions are frequentlydefined, we may abbreviate(define id (lambda (args) (body)) )as(define (id args) (body) )Thus(define (plus1 x) (+ x


View Full Document

UW-Madison COMPSCI 538 - Lecture 9 Notes

Download Lecture 9 Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 9 Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 9 Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?