14-October-2002cse413-07-Lists © 2002 University of Washington1 Lists 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

1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
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.
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.
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
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.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
The Scheme Programming Language History and Significance Dmitry Nesvizhsky CIS24 Professor Danny Kopec.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Functional Programming in Scheme
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Fall 2008Programming Development Techniques 1 Topic 5 Data Abstraction Note: This represents a change in order. We are skipping to chapter 2 without finishing.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Functional Programming: Lisp MacLennan Chapter 10.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
9-October-2002cse MoreLambda © 2002 University of Washington1 More Lambda CSE 413, Autumn 2002 Programming Languages
Forms Writing your own procedures CS 480/680 – Comparative Languages.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Comparative Programming Languages Functional programming with Lisp/Scheme.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
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.
Abstraction A way of managing complexity for large programs A means of separating details of computation from use of computation Types of Abstraction Data.
Functional Programming
Functional Programming
CS 550 Programming Languages Jeremy Johnson
CS 326 Programming Languages, Concepts and Implementation
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
Racket CSC270 Pepper major portions credited to
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lists in Lisp and Scheme
CS 270 Math Foundations of CS Jeremy Johnson
COP4020 Programming Languages
6.001 SICP Data abstractions
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
The Metacircular Evaluator
Scheme: Basic Functionality
Abstraction and Repetition
The Metacircular Evaluator (Continued)
Streams, Delayed Evaluation and a Normal Order Interpreter
6.001 SICP Variations on a Scheme
Announcements Quiz 5 HW6 due October 23
Lecture #7 מבוא מורחב.
List and list operations (continue).
Abstraction and Repetition
Today’s topics Abstractions Procedural Data
Functional Programming: Lisp
Lecture # , , , , מבוא מורחב.
Common Lisp II.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
More Scheme CS 331.
Defining Macros in Scheme
Abstraction and Repetition
Presentation transcript:

14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages

14-October-2002cse Lists © 2002 University of Washington2 Readings and References Reading »Sections , Structure and Interpretation of Computer Programs, by Abelson, Sussman, and Sussman Other References »Section 6.3.2, Revised 5 Report on the Algorithmic Language Scheme (R5RS)

14-October-2002cse Lists © 2002 University of Washington3 Pairs are the glue Using cons to build pairs, we can build data structures of unlimited complexity We can roll our own »if not too complex or if performance issues We can adopt a standard and use it for the basic elements of more complex structures »lists

14-October-2002cse Lists © 2002 University of Washington4 Rational numbers with pairs An example of a fairly simple data structure that could be built directly with pairs (define (make-rat n d) (cons n d)) (define (numer x) (car x)) (define (denom x) (cdr x)) (make-rat 1 2) 12

14-October-2002cse Lists © 2002 University of Washington5 Extensibility What if we want to extend the data structure somehow? What if we want to define a structure that has more than two elements? We can use the pairs to glue pairs together in a more general fashion and so allow more general constructions »Lists

14-October-2002cse Lists © 2002 University of Washington6 Fundamental list structure By convention, a list is a sequence of linked pairs »car of each pair is the data element »cdr of each pair points to list tail or the empty list e 1 2 3

14-October-2002cse Lists © 2002 University of Washington7 List construction (define e (cons 1 (cons 2 (cons 3 '())))) e (define e (list 1 2 3))

14-October-2002cse Lists © 2002 University of Washington8 procedure list (list a b c...) list returns a newly allocated list of its arguments »the arguments can be atomic items like numbers or quoted symbols »the arguments can be other lists The backbone structure of a list is always the same »a sequence of linked pairs, ending with a pointer to null (the empty list) »the car element of each pair is the list item »the list items can be other lists

14-October-2002cse Lists © 2002 University of Washington9 List structure (define a (list 4 5 6)) a (define b (list 7 a 8)) a 7 8 b

14-October-2002cse Lists © 2002 University of Washington10 Rational numbers with lists (define (make-rat n d) (list n d)) (define (numer x) (car x)) (define (denom x) (cadr x)) 1 2 (make-rat 1 2)

14-October-2002cse Lists © 2002 University of Washington11 Examples of list building (list 1 2) (cons 1 (cons 2 '())) 1 2 (cons 1 (list 2))

14-October-2002cse Lists © 2002 University of Washington12 Lists and recursion A list is zero or more connected pairs Each node is a pair Thus the parts of a list (this pair, following pairs) are lists And so recursion is a natural way to express list operations

14-October-2002cse Lists © 2002 University of Washington13 (define (length m) (if (null? m) 0 (+ 1 (length (cdr m))))) cdr down We can process each element in turn by processing the first element in the list, then recursively processing the rest of the list base case reduction step

14-October-2002cse Lists © 2002 University of Washington14 sum the items in a list (add-items (list 2 5 4)) (define (add-items m) (if (null? m) 0 (+ (car m) (add-items (cdr m))))) (+ 2 (+ 5 (+ 4 0)))

14-October-2002cse Lists © 2002 University of Washington15 (define (reverse m) (define (iter shrnk grow) (if (null? shrnk) grow (iter (cdr shrnk) (cons (car shrnk) grow)))) (iter m '())) cons up We can build a list to return to the caller piece by piece as we go along through the input list

14-October-2002cse Lists © 2002 University of Washington16 multiply each list element by 2 (define (double-all m) (if (null? m) '() (cons (* 2 (car m)) (double-all (cdr m))))) (double-all (list )) (cons 8 (cons 0 (cons -6 '())))

14-October-2002cse Lists © 2002 University of Washington17 Variable number of arguments We can define a procedure that has zero or more required parameters, plus provision for a variable number of parameters to follow »The required parameters are named in the define statement as usual »They are followed by a "." and a single parameter name At runtime, the single parameter name will be given a list of all the remaining actual parameter values

14-October-2002cse Lists © 2002 University of Washington18 (same-parity x. y) (define (same-parity x. y) … > (same-parity ) ( ) > (same-parity ) (2 4 6) > The first argument value is assigned to x, all the rest are assigned as a list to y

14-October-2002cse Lists © 2002 University of Washington19 map We can use the general purpose function map to map over the elements of a list and apply some function to them (define (map p m) (if (null? m) '() (cons (p (car m)) (map p (cdr m))))) (define (double-all m) (map (lambda (x) (* 2 x)) m))