6.001 SICP 1 6.001 SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.

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.
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.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
6.001 SICP SICP – September Introduction Trevor Darrell 32-D web page:
1 The Metacircular Evaluator Chapter 4 Section 4.1 We will not cover every little detail in the lectures, so you MUST read Section 4.1 in full.
SICP Symbolic data Symbol: a primitive type Generalization Symbolic differentiation.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
SICP Data Mutation Primitive and Compound Data Mutators Stack Example non-mutating mutating Queue Example non-mutating mutating.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Programming Languages I LISP
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
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.
6.001 SICP 1/ : Structure and Interpretation of Computer Programs Symbols Example of using symbols Differentiation.
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Scheme & Functional Programming. ( ) >> 64 ( ) >> 666 (* ) >> 1200 (+ (* 3 5) (- 10 6)) >> 19.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
מבוא מורחב 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 한 태숙.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
You can access the members of a list with the functions car (or first) and cdr (or rest): (setf list '(a b c)) (car list) ⇒ a (first list) ⇒ a (cdr list)
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
1 Scheme (Section 11.2) CSCI 431 Programming Languages Fall 2003.
Comparative Programming Languages Functional programming with Lisp/Scheme.
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
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.
CS314 – Section 5 Recitation 9
Additional Scheme examples
Edited by Original material by Eric Grimson
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
6.001 SICP Object Oriented Programming
Introduction to Scheme
Chapter 15 – Functional Programming Languages
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Lists in Lisp and Scheme
Original material by Eric Grimson
2.3 Representation Strategies for Data Types
The Metacircular Evaluator
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Lecture #9 מבוא מורחב.
Data Mutation Primitive and compound data mutators set! for names
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
overview today’s ideas set-car! and set-cdr!
6.001 SICP Variations on a Scheme
Mutators for compound data Stack Queue
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
Announcements Quiz 5 HW6 due October 23
6.001 SICP Interpretation Parts of an interpreter
Lecture 13: Assignment and the Environment Model (EM)
Modern Programming Languages Lecture 18 Fakhar Lodhi
topics interpreters meta-linguistic abstraction eval and apply
Lisp.
Lists in Lisp and Scheme
Presentation transcript:

6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz

6.001 SICP 2 Quote Symbolic algebra, e.g. “the morning star” is “the evening star”, “the morning star” is “venus” ==> “the evening star” is “venus”. Use symbols to build a language about computation, i.e. a programming language, i.e. scheme…

6.001 SICP 3 Quote quote evaluation rules: (quote (a b)) ==> (list (quote a) (quote b)) (quote ()) ==> nil (quote ) ==> quote syntactic sugar: ‘(a b) == (quote (a b))

6.001 SICP 4 Quote vs. List When to use list and when to use quote ? What is the difference between: (list 1 2 3) ‘(1 2 3) or between, with (define x 20) (list (+ x 1) (+ x 2) (+ x 3)) ‘((+ x 1) (+ x 2) (+ x 3)) Given (define x 20), what is (+ ‘x 5) ?

6.001 SICP 5 Quote Draw box and pointer diagram for (list ‘a ‘b ‘c) (cons 'a '(b)) (cons '(a) '(b)) (list 'a 'b) (list '(a) '(b)) (append '(a) '(b))

6.001 SICP 6 Recitation problem What is the value printed if you evaluate the following two expressions ( list (+ 3 4) '(+ 3 4) '(list 3 4)) ==> ?? ''x ==> ?? You observe the following behavior (cons 1 nil) ==> (1) (list 1) ==> (1) (eq? (cons 1 nil) (list 1)) ==> #f Why does eq? return false?

6.001 SICP 7 Drill what will the following expressions print? (list 'a b) (a 4) '(a b) (a b) (cons b d) (4 a b) (list 'b c) (b (5 6)) (car d) a (define a 3) (define b 4) (define c (list 5 6)) (define d '(a b)) (define p (list cons +)) (define q '(cons +)) (define r (list 'cons '+))

6.001 SICP 8 Drill what will the following expressions print? ((car p) 3 4) (3. 4) ((cadr p) 3 4) 7 ((car r) 3 4) error -- can't apply a symbol ((cadr q) 3 4) error -- can't apply a symbol (car ''a) quote (define a 3) (define b 4) (define c (list 5 6)) (define d '(a b)) (define p (list cons +)) (define q '(cons +)) (define r (list 'cons '+))

6.001 SICP 9 Symbols vs. strings Strings are for data, input/output messages, etc String literals such as “help” is not a symbol Rather they are self-evaluating expressions (like numbers) Symbols occupy constant space and can be compared in constant time no matter how long their printed representation is

6.001 SICP 10 Eq? Equal? and = Informally, = tests if two numbers are the same equal? tests if two expressions have same printed representation eq? tests if two symbols are equal also tests if two pairs are “identical” e.g., came from the same cons operation / occupies the same memory location…

6.001 SICP 11 equality (define x (list 1 2)) (define y (list x x)) (define z (list (list 1 2) (list 1 2)) (equal? (car y) (cadr y)) ==> #t (eq? (car y) (cadr y)) ==> #t (equal? (car z) (cadr z)) ==> #t (eq? (car z) (cadr z)) ==> #f

6.001 SICP 12 equal? Write the function equal? that takes two trees of symbols and returns true if the same symbols are arranged in the same structure. (equal? '(this is a list) '(this is a list)) ;Value: #t (equal? '(this (is a) list) '(this (is a) list)) ;Value: #t (equal? '(this is a list) '(this (is a) list)) ;Value: #f

6.001 SICP 13 equal? (define (equal? a b) (or (and (null? a) (null? b)) (and (symbol? a) (symbol? b) (eq? a b)) (and (pair? a) (pair? b) (equal? (car a) (car b)) (equal? (cdr a) (cdr b)))))

6.001 SICP 14 Self-printer Write a scheme expression that print itself. Tips: 1 - copy the expression, 2 - implement the copier

6.001 SICP 15 Self-printer ((lambda (x) (list x (list (quote quote) x))) (quote (lambda (x) (list x (list (quote quote) x)))))