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

Slides:



Advertisements
Similar presentations
Lisp Control and Data Structures CIS 479/579 Bruce R. Maxim UM-Dearborn.
Advertisements

CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
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.
Writing LISP functions. 2 COND Rule 1: Unless the function is extremely simple, begin with a COND If you can write the function body in one line, do it.
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.
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.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
Natural Numbers or, "All you ever need is compounds, variants, and recursion".
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
Creating Functional Programs Brad Vander Zanden. Basic Techniques Tail Recursion – Use continuation arguments if necessary – Akin to pre-processing a.
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
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.
Common Lisp! John Paxton Montana State University Summer 2003.
>(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?
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
Scheme examples. Recursion Iteration is achieved via recursion Selection is achieved via COND Review the following examples to learn to think recursively.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
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.
RECURSIVE PATTERNS WRITE A START VALUE… THEN WRITE THE PATTERN USING THE WORDS NOW AND NEXT: NEXT = NOW _________.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
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.
Final Exam ~ notes On the semester’s material, with an attempt at bridging different topics. You may use two double-sided pages of notes (similar to before)…
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
1 Append: process  (append list1 list2) (cons 1 (append ‘(2) list2)) (cons 1 (cons 2 (append ‘() list2))) (cons 1 (cons 2 list2)) (define (append list1.
Practice session #6: 1. Sequence operations 2. Partial evaluation with currying 3. Lazy-lists.
Clojure 2 Feb 7,
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.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
2.4 Gray Code ReturnNext In a Gray code only one bit changes between each pair of successive code words. Gray code is a reflected code. It can be defined.
1 Proving Properties of Recursive List Functions CS 270 Math Foundations of CS Jeremy Johnson.
1 Proving Properties of Recursive Functions and Data Structures CS 270 Math Foundations of CS Jeremy Johnson.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
Functional Programming
Lets and Loops Tail Recursion.
Additional Scheme examples
CS 550 Programming Languages Jeremy Johnson
Class 11: Two-argument recursion
Modern Programming Languages Lecture 20 Fakhar Lodhi
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
Get Help-Gmail help number
Recursion.
Proving Properties of Recursive List Functions
Recursion & Linked Lists
PROGRAMMING IN HASKELL
Lecture #8 מבוא מורחב.
Modern Programming Languages Lecture 20 Fakhar Lodhi
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
6.001 SICP Data abstractions
List and list operations (continue).
Lecture # , , , , מבוא מורחב.
Programming Languages
Lisp.
Recursion Exercises.
Dispatch on Type (one-less x) => x-1 if x is a <number>
Presentation transcript:

Joshua Eckroth

The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the call stack. 5.Discover tail-call optimization.

L = (first L) (rest L)

L = (cons 'a L) 'a

Review: Length of L (define (length L) (cond [(null? L) 0] [else (+ 1 (length (rest L)))]))

Review: Is e a member of L? (define (member e L) (cond [(null? L) #f] [(equal? e (first L)) #t] [else (member e (rest L))]))

Review: Remove first e from L (define (remove e L) (cond [(null? L) '()] [(equal? e (first L)) (rest L)] [else (cons (first L) (remove e (rest L)))]))

Task: Reverse L (define (reverse L) (cond [__________ __________] [________ _______________________]))

Task: Reverse L (define (reverse L) (cond [(null? L) '()] [else (append (reverse (rest L)) (list (first L)))]))

Task: Write the range function (define (range lo hi) (cond [_________ ___________] [______ ___________________________]))

Task: Write the range function (define (range lo hi) (cond [(> lo hi) '()] ;; or >= [else (cons lo (range (+ 1 lo) hi))]))

Recursion in nature.

A list is the empty list (null), OR, a first value, followed by a list (rest).

0 = {} n + 1 = n U {n} 1 = {0} = {{}} 2 = {0, 1} = {{}, {{}}} 3 = {0, 1, 2} = {{}, {{}}, {{}, {{}}}} The Natural Numbers

Derivatives Sum rule: Product rule: Chain rule: If, Then,

def foobar(x): if x < 10: for y in baz: while x > 0: if z == y: print Too much nesting! (define (foobar x) (if (< x 10) (for ([y baz]) (do () (<= x 0) (if (= z y) (display Too much nesting!)))))) Languages

I ate a banana. I know that I wish I ate a banana. I wish I ate a banana. Susans brothers wifes sisters cats favorite toy…

… language makes infinite use of finite means … - Wilhelm von Humboldt

[T]he only reason language needs to be recursive is because its function is to express recursive thoughts. If there were not any recursive thoughts, the means of expression would not need recursion either. - Pinker & Jackendoff, The faculty of language: Whats special about it? Cognition, 2005, 95(2), pp

The thinker thinks of thinking of thinking. Corballis, The Recursive Mind: The Origins of Human Language, Thought, and Civilization, Princeton University Press, 2011

More Perlisisms: Recursion is the root of computation since it trades description for time. - Alan Perlis

Ackermann function Looks harmless!

Ackermann function (define (ackermann m n) (cond [(= m 0) (+ 1 n)] [(= n 0) (ackermann (- m 1) 1)] [else (ackermann (- m 1) (ackermann m (- n 1)))]))

A(2, 2) =

A(2, 2) = 9

I count 27 function calls.

A(3, 2) =

A(3, 2) = 29

540 calls!

A(4, 2) =

Why does recursion get a bad rap?

Stack frames

Task: Flatten L (define (flatten L) (cond [_________ ____] [______________ (append ____________________ ____________________)] [______ ________________________________ ]))

Task: Flatten L (define (flatten L) (cond [(null? L) '()] [(list? (first L)) (append (flatten (first L)) (flatten (rest L)))] [else (cons (first L) (flatten (rest L)))]))

Quiz: Fill in details for flattens call stack.

Tail-recursion Sometimes, recursion can be both fast and expressive.

Quiz: Identify tail-calls. (define (example L) (if (= 1 0) #f (example (rest L)))) (define (bloop a b c) (cond [(bloop 1 2 3) (bloop 3 2 1)] [else (rest (bloop 0 0 0))]))

Quiz: Identify tail-calls. (define (example L) (if (= 1 0) #f (example (rest L)))) (define (bloop a b c) (cond [(bloop 1 2 3) (bloop 3 2 1)] [else (rest (bloop 0 0 0))]))

Quiz: Identify tail-calls. (define (example L) (if (= 1 0) #f (example (rest L)))) (define (bloop a b c) (cond [(bloop 1 2 3) (bloop 3 2 1)] [else (rest (bloop 0 0 0))]))

Review: Length of L (define (length L) (cond [(null? L) 0] [else (+ 1 (length (rest L)))]))

A tail-recursive length (define (lengthT L Acc) (cond [(null? L) ________] [else (lengthT (rest L) ____________)]))

A tail-recursive length (define (lengthT L Acc) (cond [(null? L) Acc] [else (lengthT (rest L) (+ 1 Acc)])) (define (length L) (length L 0))

Review: Remove first e from L (define (remove e L) (cond [(null? L) '()] [(equal? e (first L)) (rest L)] [else (cons (first L) (remove e (rest L)))]))

A tail-recursive remove (define (removeT e L Acc) (cond [(null? L) _______] [(equal? (first L) e) __________________________] [else (remove e (rest L) ______________________])) (define (remove e L) (remove e L '()))

A tail-recursive remove (define (removeT e L Acc) (cond [(null? L) Acc] [(equal? (first L) e) (append Acc (rest L))] [else (remove e (rest L) (cons (first L) Acc))])) (define (remove e L) (remove e L '()))

Quiz: Write reverse and range with tail-recursion.

Tail-call optimization keeps a small stack.

Recursion is the root of computation since it trades description for time, but not necessarily.