18-October-2002cse413-09-Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages

Slides:



Advertisements
Similar presentations
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Advertisements

Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
SICP Symbolic data Symbol: a primitive type Generalization Symbolic differentiation.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Tree Traversal. Traversal Algorithms preorder inorder postorder.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
1 CS308 Data Structures An application of binary trees: Binary Expression Trees.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
6.001 SICP 1/ : Structure and Interpretation of Computer Programs Symbols Example of using symbols Differentiation.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 15 Functional Programming Languages.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
Lecture 4: Metacircles Eval Apply David Evans
Additional Scheme examples
History of Computing – Lisp
Introduction to Scheme
CS 326 Programming Languages, Concepts and Implementation
Chapter 15 – Functional Programming Languages
Lists in Lisp and Scheme
COP4020 Programming Languages
The Metacircular Evaluator
The Metacircular Evaluator
CS 36 – Chapter 11 Functional programming Features Practice
Scheme: Basic Functionality
Abstraction and Repetition
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Section 9.3 by Andrew Watkins
Lecture #9 מבוא מורחב.
6.001 SICP Variations on a Scheme
Announcements Quiz 5 HW6 due October 23
Abstraction and Repetition
Functional Programming: Lisp
6.001 SICP Interpretation Parts of an interpreter
topics interpreters meta-linguistic abstraction eval and apply
Lisp.
Abstraction and Repetition
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages

18-October-2002cse Symbols © 2002 University of Washington2 Readings and References Reading »Section 2.3.1, Structure and Interpretation of Computer Programs, by Abelson, Sussman, and Sussman Other References »Sections 4.1.2, 6.1, 6.3.3, Revised 5 Report on the Algorithmic Language Scheme (R5RS)

18-October-2002cse Symbols © 2002 University of Washington3 Evaluating symbols and expressions We've been using symbols and lists of symbols to refer to values of all kinds in our programs Scheme evaluates the symbols and lists that we give it »numbers evaluate to themselves »symbols evaluate to their current value »lists are evaluated as expressions defining procedure calls on a sets of actual arguments (+ a 3) (inc b)

18-October-2002cse Symbols © 2002 University of Washington4 Manipulating symbols, not values What if we want to manipulate the symbols, and not the value of the symbols »perhaps evaluate after all the manipulation is done We need a way to say "use this symbol or list as it is, don’t evaluate it" Special form quote >(define a 1) >a=> 1 >(quote a)=> a

18-October-2002cse Symbols © 2002 University of Washington5 Special form: quote (quote  datum  ) or '  datum  This expression always evaluates to datum »datum is the external representation of the object The quote form tells Scheme to treat the given expression as a data object directly, rather than as an expression to be evaluated

18-October-2002cse Symbols © 2002 University of Washington6 Quote examples (define a 1) a=> 1 (quote a)=> a (define b (+ a a)) b=> 2 (define c (quote (+ a b))) c=> (+ a b) (car c)=> + (cadr c)=> a (caddr c)=> b a is a symbol whose value is the number 1 b is a symbol whose value is the number 2 c is a symbol whose value is the list (+ a b)

18-October-2002cse Symbols © 2002 University of Washington7 quote can be abbreviated: ' 'a=> a '(+ a b)=> (+ a b) '()=> () (null? '())=> #t '(1 (2 3) 4)=> (1 (2 3) 4) '(a (b (c)))=> (a (b (c))) (car '(1 (2 3) 4))=> 1 (cdr '(1 (2 3) 4))=> ((2 3) 4) a single quote has the exact same effect as the quote form lists are easily expressed as quoted objects

18-October-2002cse Symbols © 2002 University of Washington8 Building lists with symbols What would the interpreter print in response to evaluating each of the following expressions? (list 'a 'b) (cons 'a (list 'b)) (cons 'a (cons 'b '())) (cons 'a '(b)) '(a b) a b (a b)

18-October-2002cse Symbols © 2002 University of Washington9 Building lists with symbols What would the interpreter print in response to evaluating each of the following expressions? (cons '(a) '(b)) (list '(a) '(b)) b ((a) b) a ((a) (b)) a b

18-October-2002cse Symbols © 2002 University of Washington10 Comparing items Scheme provides several different means of comparing objects »Do two numbers have the same value? (= a b) »Are two objects the same object? (eq? a b), (eqv? a b) »Are the corresponding elements the same objects? Comparison is done recursively if elements are lists. (equal? list-a list-b)

18-October-2002cse Symbols © 2002 University of Washington11 (member item s) ; find an item of any kind in a list s ; return the sublist that starts with the item ; or return #f (define (member item s) (cond ((null? s) #f) ((equal? item (car s)) s) (else (member item (cdr s))))) (member 'a '(c d a)) => (a) (member '(1 3) '(1 (1 3) 3)) => ((1 3) 3) (member 'b '(a (b) c)) => #f

18-October-2002cse Symbols © 2002 University of Washington12 Recall: Expression tree example (1 + ( 2 * ( ))) infix notation (+ 1 (* 2 (- 3 5))) Scheme expression + 1 * expression tree

18-October-2002cse Symbols © 2002 University of Washington13 Represent expression with a list Each node is represented by a 3-element list »(operator left-operand right-operand) Operands can be »numbers (explicit values) »other expressions (lists) In previous implementation, operators were the actual procedures »This time, we will use symbols throughout

18-October-2002cse Symbols © 2002 University of Washington14 Expressions as trees, trees as lists + 1 * logical expression tree '(+ 1 (* 2 (- 3 5))) + our data structure (1+(2*(3-5))) 1 *

18-October-2002cse Symbols © 2002 University of Washington15 eval-expr (define (eval-op op) (cond ((eq? op '+) +) ((eq? op '-) -) ((eq? op '/) /) ((eq? op '*) *))) (define (eval-expr exp) (if (not (pair? exp)) exp ((eval-op (operator exp)) (eval-expr (left exp)) (eval-expr (right exp))))) (eval-expr '(+ 1 2))

18-October-2002cse Symbols © 2002 University of Washington16 evaluator (define (evaluator exp) (if (not (pair? exp)) exp ((eval (operator exp)) (eval-expr (left exp)) (eval-expr (right exp))))) (evaluator '(+ 1 2))

18-October-2002cse Symbols © 2002 University of Washington17 Traversing a binary tree Recall the definitions of traversal »pre-order this node, left branch, right branch »in-order left branch, this node, right branch »post-order left branch, right branch, this node + 1 * (1+(2*(3-5)))

18-October-2002cse Symbols © 2002 University of Washington18 Traverse the expression tree (define (in-order exp) (if (not (pair? exp)) (list exp) (append (in-order (left exp)) (list (operator exp)) (in-order (right exp)) ))) (define (post-order exp) (if (not (pair? exp)) (list exp) (append (post-order (left exp)) (post-order (right exp)) (list (operator exp))))) (define f '(+ 1 (* 2 (- 3 5)))) (in-order f) (1 + 2 * 3 - 5) (post-order f) ( * +)