Lecture 8: Recursion Practice CS200: Computer Science

Slides:



Advertisements
Similar presentations
Cs1120 Fall 2009 David Evans Lecture 11: Generalizing List Procedures 1.
Advertisements

Class 21: Imperative Programming University of Virginia cs1120 David Evans.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 13: Of On and Off Grounds Sorting.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Cs1120 Fall 2009 David Evans Lecture 15: Running Practice.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 18: The Story So Far.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 16: Quicker Sorting.
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 26: Printing and Stuff.
6.001 SICP SICP – October Trees Trevor Darrell 32-D512 Office Hour: W web page:
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 14: Asymptotic Growth.
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 11: 1% Pure Luck Make-up lab hours:
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
David Evans Class 12: Quickest Sorting CS150: Computer Science University of Virginia Computer Science Rose Bush by Jacintha.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
David Evans Class 13: Quicksort, Problems and Procedures CS150: Computer Science University of Virginia Computer Science.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: Decrypting Work Circle Fractal.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 11: CS Logo, by Lincoln Hamilton and.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
David Evans Class 20: Quick Sorting CS200: Computer Science University of Virginia Computer Science Queen’s University,
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 10: Puzzling Pegboards.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 9: Strange Loops and Sinister Repeaters.
David Evans Class 21: The Story So Far (Quicksort, Continuing Golden Ages) CS200: Computer Science University of Virginia.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: QuickSorting Queen’s University,
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Of On and Off Grounds Sorting Coffee.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 6. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Class 7: List Procedures David Evans cs1120 Fall 2009.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 14: P = NP?
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: Programming with Data.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 8: Cons car cdr sdr wdr.
Lecture 4: Metacircles Eval Apply David Evans
Additional Scheme examples
Lecture 17: Environments CS200: Computer Science
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Lecture 13: Quicksorting CS200: Computer Science
PPL Lazy Lists.
Lecture 7: List Recursion CS200: Computer Science
Lecture 16: Quickest Sorting CS150: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
Lecture 11: All Sorts CS200: Computer Science University of Virginia
David Evans Lecture 9: The Great Lambda Tree of Infinite Knowledge and Ultimate Power CS200: Computer Science University.
Lecture 13: Cost of Sorts CS150: Computer Science
Lecture #8 מבוא מורחב.
Lecture 22: P = NP? CS200: Computer Science University of Virginia
Lecture 10: Quicker Sorting CS150: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 9: The Great Lambda Tree of Knowledge and Power
Lecture #7 מבוא מורחב.
List and list operations (continue).
Lecture # , , , , מבוא מורחב.
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

David Evans http://www.cs.virginia.edu/evans Lecture 8: Recursion Practice CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans

Simpler Gauss Sum? > (intsto 5) (1 2 3 4 5) > (intsto 1) (1) (define (insertl lst f stopval) (if (null? lst) stopval (f (car lst) (insertl (cdr lst) f stopval)))) > (intsto 5) (1 2 3 4 5) > (intsto 1) (1) Same as previous lecture. (define (gauss-sum n) (insertl (intsto n) + 0)) > (gauss-sum 10) 55 (insertl (list 1 2 3 … n) + 0)  (+ 1 (+ 2 (+ 3 (+ … (+ n 0))))) 2 February 2004 CS 200 Spring 2004

Reverse intsto (define (intsfrom n) (if (= n 0) null ;;; Evaluates to the list (n n-1 … 3 2 1) ;;; if n >= 1, null if n = 0. > (intsfrom 10) (10 9 8 7 6 5 4 3 2 1) (define (intsfrom n) (if (= n 0) null (cons n (intsfrom (- n 1))))) 2 February 2004 CS 200 Spring 2004

Intsto? (define (intsto n) (if (= n 0) null (list (intsto (- n 1)) n))) > (intsto 5) (((((() 1) 2) 3) 4) 5) 2 February 2004 CS 200 Spring 2004

append > (append (list 3) (list 4)) (3 4) > (append (list 3) null) (3) > (append null (list 3)) > (append (list 1 2 3) (list 4 5 6)) (1 2 3 4 5 6) Checkup: Try and define append yourself. 2 February 2004 CS 200 Spring 2004

Intsto (define (intsto n) (if (= n 0) null (append (intsto (- n 1)) (list n)))) > (intsto 5) (1 2 3 4 5) 2 February 2004 CS 200 Spring 2004

Using Intsto (define (gauss-sum n) (insertl (intsto n) + 0)) (define (factorial n) (insertl (intsto n) * 1)) 2 February 2004 CS 200 Spring 2004

map (map f lst) Evaluates to the list of values produced by applying f to each element of lst. 2 February 2004 CS 200 Spring 2004

map (define (map f lst) (if (null? lst) null (cons (f (car lst)) (map f (cdr lst)))) 2 February 2004 CS 200 Spring 2004

map (define (map f lst) (insertl lst (lambda (a b) (cons (f a) b)) null)) 2 February 2004 CS 200 Spring 2004

map examples > (map (lambda (x) x) (intsto 5)) (1 2 3 4 5) > (map (lambda (x) (* x x)) (intsto 5)) (1 4 9 16 25) > (map (lambda (row) (display-one-row output-file row tile-width tile-height)) tiles) Displays a photomosaic! 2 February 2004 CS 200 Spring 2004

Challenge Problem Define a procedure (define (for index test combine next accum) …) that can be used like this: (define (gauss-sum n) (for 1 (lambda (index) (> index n)) + (lambda (x) (+ 1 x)) 0)) 2 February 2004 CS 200 Spring 2004