Structuring data So far we’ve seen two types of values that we can bind to names: –numbers –Functions Today we will see two more: –symbols –pairs.

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.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
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.
6.001: Structure and Interpretation of Computer Programs Symbols Quotation Relevant details of the reader Example of using symbols Alists Differentiation.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Fall 2008Programming Development Techniques 1 Topic 19 Mutable Data Objects Section 3.3.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Metacircular Evaluator 4.1, pages definitions file on web 2.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
SICP Data abstraction revisited Data structures: association list, vector, hash table Table abstract data type No implementation of an ADT is necessarily.
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
SICP Data Mutation Primitive and Compound Data Mutators Stack Example non-mutating mutating Queue Example non-mutating mutating.
Mutation So far, our data abstractions consists of the following: –Constructors –Selectors/Accessors –Operations –Contract Once a data object is created,
1 Functional languages (e.g. Scheme, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
1 Course progression Parallels between languages, including –sequencing, selection and repetition –abstraction mechanisms behavioral abstraction (e.g.
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.
Scheme in Scheme 1. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
CSE 341 Programming Languages Racket Datatype Style Programming Zach Tatlock Spring 2014.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
1 Data abstraction, revisited Design tradeoffs: Speed vs robustness modularity ease of maintenance Table abstract data type: 3 versions No implementation.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1/32 Data Mutation Primitive and compound data mutators set! for names set-car!, set-cdr! for pairs Stack example non-mutating mutating Queue example non-mutating.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CS61A Lecture Colleen Lewis. Clicker poll Where do you think you’re learning the most? I assume you’ll learn the most in lab & disc (so.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
6.001 SICP Variations on a Scheme
CS 326 Programming Languages, Concepts and Implementation
6.001: Structure and Interpretation of Computer Programs
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
The Metacircular Evaluator
Data abstraction, revisited
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
Scheme: Basic Functionality
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture #9 מבוא מורחב.
Data Mutation Primitive and compound data mutators set! for names
6.001 SICP Variations on a Scheme
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lisp: Representation of Data
Defining Functions with DEFUN
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
Functional Programming: Lisp
6.001 SICP Interpretation Parts of an interpreter
Lisp.
Lisp: Representation of Data
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Presentation transcript:

Structuring data So far we’ve seen two types of values that we can bind to names: –numbers –Functions Today we will see two more: –symbols –pairs

Symbols A symbol is an unevaluated name. Consider the following interaction: > (define x 12) > x 12 The x in the define is not evaluated. It is a symbol. The x in the second line is evaluated by looking up the value it is bound to in the current environment.

Using symbols We can bind a name to a symbolic value by quoting the symbol – quoting prevents evaluation: > (define fifi 'poodle) > fifi poodle The quote mark (') is syntactic sugar for the form (quote …); the above is equivalent to: > (define fifi (quote poodle))

Pairs cons is a primitive function which builds a pair. A pair has two members. (cons 'a 'b) builds a pair –whose first member is the symbol a –whose second member is the symbol b cons is therefore a pair constructor. Graphically: ba

accessors car returns the first member of a pair cdr returns the second member of a pair > (define myPair (cons 'a 'b)) > (car myPair) a > (cdr myPair) b

How is a pair displayed? > (define myPair (cons 'a 'b)) > myPair (a. b) Pairs are printed with a dot (.) separating the first (left) and second (right) components.

The REPL When interacting with a Scheme system, a Read-Eval-Print Loop (REPL) reads what you type, evaluates it, prints the resulting value, and loops back to repeat. The reader and printer parts of the REPL handle syntactic sugar (like converting 'a into (quote a)) and also printing things like lists.

Lists What is a list? A recursive definition: –the empty list () is a list –a pair whose cdr is a list is a list We can build a list using cons and (): > (define myList (cons 'a '())) > myList (a)

Wait a minute!! > (define myPair (cons 'a 'b)) > myPair (a. b) > (define myList (cons 'a '()) > myList (a)

What's going on? Why did myList print as (a) rather than as (a. ())? In fact, they are entirely equivalent: the REPL strikes again. The printer formats pairs which cdr is a pair in a special way to make lists easier to read.

Printing lists (a. (b. (c. (d. ())))) is printed as (a b c d) Graphically the structure is: This structure can be built in a variety of ways, some of which are shown on the next slide. abcd

Choices…choices (cons 'a (cons 'b (cons 'c (cons 'd '())))) (list 'a 'b 'c 'd) '(a b c d) '(a. (b c d)) (cons 'a (list 'b 'c 'd)) and so on!

Association lists An association list is a list of pairs. Used to implement a look-up table of key- value pairs. The primitive assoc is used to search an association list, given a key. Example on next slide.

> (define aList '((fifi. poodle) (fido. bulldog) (clifford. bigRed) (lassie. collie))) > (assoc 'fido aList) (fido. bulldog) > (assoc 'rufus aList) #f

Mutating pair components set-car! mutates the value of the car of a pair set-cdr! mutates the value of the cdr of a pair > (define myPair (cons 'a 'b)) > myPair (a. b) > (set-car! myPair 'fred) > myPair (fred. b) > (set-cdr! myPair 'wilma) > myPair (fred. wilma)