CS 550 Programming Languages Jeremy Johnson

Slides:



Advertisements
Similar presentations
Joshua Eckroth The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the.
Advertisements

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.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
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.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
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.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
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.
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.
Data Abstraction… The truth comes out…. What we’re doing today… Abstraction ADT: Dotted Pair ADT: List Box and Pointer List Recursion Deep List Recursion.
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
>(setf oldlist ) Constructing a list We know how to make a list in lisp; we simply write it: ‘4321( ) What if we want a new list with that list as a part?
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 18 / 2008 Instructor: Michael Eckmann.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
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.
Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office.
The ACL2 Proof Assistant Formal Methods Jeremy Johnson.
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.
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
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.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
Comparative Programming Languages Functional programming with Lisp/Scheme.
Spring 16 CSCI 4430, A Milanova/BG Ryder 1 Announcements HW5 due March 28 Homework Server link is up I will have office hours, Fri or Mon, check Announcements.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
1 Proving Properties of Recursive List Functions CS 270 Math Foundations of CS Jeremy Johnson.
1 Programming Languages (CS 550) List Processing, Dynamic Memory Allocation and Garbage Collection Jeremy R. Johnson.
1 Recursive Data Structures CS 270 Math Foundations of CS Jeremy Johnson.
CS314 – Section 5 Recitation 10
Class 2 – Recursion on Lists
Additional Scheme examples
Modern Programming Languages Lecture 20 Fakhar Lodhi
CS 326 Programming Languages, Concepts and Implementation
Racket CSC270 Pepper major portions credited to
Chapter 15 – Functional Programming Languages
Lists in Lisp and Scheme
CS 270 Math Foundations of CS Jeremy Johnson
Introduction to Functional Programming in Racket
Nondeterministic Evaluation
Proving Properties of Recursive Functions and Data Structures
6.001 SICP Data abstractions
Proving Properties of Recursive List Functions
Mini Language Interpreter Programming Languages (CS 550)
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Lecture #8 מבוא מורחב.
Modern Programming Languages Lecture 20 Fakhar Lodhi
Streams, Delayed Evaluation and a Normal Order Interpreter
CS 457/557: Functional Languages Folds
Introduction to Functional Programming in Racket
Announcements Quiz 5 HW6 due October 23
Lecture #7 מבוא מורחב.
List and list operations (continue).
Lecture # , , , , מבוא מורחב.
Programming Languages
Lisp.
More Scheme CS 331.
Lists in Lisp and Scheme
Presentation transcript:

CS 550 Programming Languages Jeremy Johnson September 4, 1997 Lists and Recursion CS 550 Programming Languages Jeremy Johnson

September 4, 1997 Objective To provide a recursive definition of lists and several recursive functions for processing lists that mimic the recursive definition To practice using recursive data structures and writing recursive functions in Racket

Fundamental List Functions L = (x1 … xn) null? : All → Boolean cons? : All → Boolean cons : All × List → Cons (cons x L) = (x x1 … xn) first : Cons → All (first (cons x L)) = x rest : Cons → List (rest (cons x L)) = L

Building Lists > (list x1 … xn) [macro – variable # of args] (cons x1 (cons x2 … (cons xn nil)…)) > (cons? ‘()) => #f > (null? ‘()) => #t > (cons 1 ‘()) => ‘(1) (cons? (cons 1 ‘())) => #t (cons 1 (cons 2 (cons 3 ‘()))) => ‘(1 2 3) (list 1 2 3) => ‘(1 2 3) (cons (cons 1 ‘()) ‘()) => ‘((1))

Exercise How do you construct the list ((1) 2)?

Exercise How do you construct the list ((1) 2)? Want (cons x y) where x = ‘(1) and y = ‘(2) (cons (cons 1 ‘()) (cons 2 ‘())) => ‘((1) 2) Test this (and try additional examples) in DrRacket

More About cons A cons cell contains two fields first [also called car] rest [also called cdr] For a list the rest field must be a list Generally both fields of a cons  All (cons 1 2) = (1 . 2) Called a dotted pair

Recursive Definition Recursive Definition List : empty | cons All List Process lists using these two cases and use recursion to recursively process lists in cons Use null? to check for empty list and cons? to check for a cons Use first and rest to access components of cons

List Predicate Follow the definition (define (list? l) (if (null? l) (if (cons? l) (list? (rest l)) #f)))

Version with cond (define (list? l) (cond [(null? l) #t] [(cons? l) (list? (rest l))] [else #f]))

Non-Terminating Make sure recursive calls are smaller (define (list? l) (if (null? l) #t (if (cons? l) (list? l) #f)))

Length1 (define (length l) (if (null? l) ( ) )) ( ) )) 1: length is a built-in function in Racket

Length1 (define (length l) (if (null? l) (+ 1 (length (rest l))) )) (+ 1 (length (rest l))) )) The recursive function can be “thought of” as a definition of length 1: length is a built-in function in Racket

Member1 (define (member x l) (cond [(null? l) #f] [ ] [ ])) 1. Member is a built-in function in Racket and the specification is different

Member1 (define (member x l) (cond [(null? l) #f] [(equal? x (first l)) #t] [else (member x (rest l))])) 1. Member is a built-in function in Racket and the specification is different

Append1 (define (append x y) … ) Recurse on the first input 1. append is a built-in function in Racket

Append1 (define (append x y) (if (null? x) y (cons (first x) (append (rest x) y)))) Recurse on the first input 1. append is a built-in function in Racket

Reverse1 (define (reverse l) … ) (reverse ‘(1 2 3))  (3 2 1) 1. reverse is a built-in function in Racket

Reverse1 (define (reverse l) (if (null? l) null (append (reverse (rest l)) (cons (first l) null)))) 1. reverse is a built-in function in Racket

Tail Recursive reverse (define (reverse-aux x y) (if (null? x) y (reverse-aux (rest x) (cons (first x) y)))) (define (reverse x) (reverse-aux x null)) O(n) vs O(n2)

Numatoms (define (atom? x) (not (cons? x))) ; return number of non-null atoms in x (define (numatoms x) (cond [(null? x) 0] [(atom? x) 1] [(cons? x) (+ (numatoms (first x)) (numatoms (rest x)))]))

Shallow vs Deep Recursion Length and Member only recurse on the rest field for lists that are conses Such recursion is called shallow – it does not matter whether the lists contain atoms or lists (length ‘((1 2) 3 4)) => 3 Numatoms recurses in both the first and rest fields Such recursion is called deep – it completely traverses the list when elements are lists (numatoms ‘((1 2) 3 4)) => 4

Termination Recursive list processing functions, whether shallow or deep must eventually reach the base case. The inputs to each recursive call must have a smaller size Size is the number of cons cells in the list (< (size (first l)) (size l)) (< (size (rest l)) (size l))

Size (define (atom? x) (not (cons? x))) ; return size of x = number of cons cells in x (define (size x) (cond [(atom? x) 0] [(null? x) 0] [(cons? x) (+ 1 (size (first x)) (size (rest x)))]))

Order ; return the order = maximum depth (define (order x) (cond [(null? x) 0] [(atom? x) 0] [(cons? x) (max (+ 1 (order (first x))) (order (rest x)))]))

Order Using map/reduce ; return the order = maximum depth (define (order x) (cond [(null? x) 0] [(atom? x) 0] [(cons? x) (+ 1 (foldr max 0 (map order x)))]))

Flatten Using map/reduce ; return a first order list containing all atoms in x (define (flatten x) (cond [(null? x) x] [(atom? x) (list x)] [(cons? x) (foldr append '() (map flatten x))]))