A function (negatives L) that takes a list of numbers L and returns a list containing only negative numbers from L. A recursive example to remind you (defun.

Slides:



Advertisements
Similar presentations
Higher-Order Functions and Loops c. Kathi Fisler,
Advertisements

C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.
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)
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)
Tricks.
Helper functions: when extra arguments are needed Consider this problem: we want a function index_items that takes a list L and gives a number to each.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
Defining functions in lisp In lisp, all programming is in terms of functions A function is something which –takes some arguments as input –does some computing.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
Common Lisp! John Paxton Montana State University Summer 2003.
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
>(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?
Mapping And Iteration So far, the only Lisp mechanism we have considered, which allows an action to be performed repeatedly is recursion. Most programming.
CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, pm GPB.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
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.
CS 211 RECURSION TREES. Trees versus Linked Lists A tree is like a linked list, except instead of a single next node, it can have multiple next nodes.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
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.
Lisp: a history Developed by John McCarthy in the 1950’s. Developed by John McCarthy in the 1950’s. Only Fortran has higher “seniority” Only Fortran has.
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.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Python November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Common lisp A functional programming language. Useful URL:
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Hopefully this lesson will give you an inception of what recursion is.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
TA SURVEY Have the TA write their name on the board Fill out the survey at: The TA should walk out 5 minutes.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
Function Design in LISP. Program Files n LISP programs are plain text –DOS extensions vary; use.lsp for this course n (load “filename.lsp”) n Can use.
Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
Operating on Lists Chapter 6. Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5)
Reverse Subtraction Objectives:  do a subtract by adding  check your answer by adding.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
1 Proving Properties of Recursive List Functions CS 270 Math Foundations of CS Jeremy Johnson.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Recursion Powerful Tool
List Algorithms Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CS314 – Section 5 Recitation 10
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Questions?.
CS 550 Programming Languages Jeremy Johnson
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
Racket CSC270 Pepper major portions credited to
CS 270 Math Foundations of CS Jeremy Johnson
Algorithm Analysis CSE 2011 Winter September 2018.
Proving Properties of Recursive List Functions
List Algorithms Taken from notes by Dr. Neil Moore
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Fundamentals of Data Representation
Modern Programming Languages Lecture 20 Fakhar Lodhi
CSE S. Tanimoto Explicit Function Application
Lisp: Using Functions as Data
RECURSION Haskell.
Recursion Taken from notes by Dr. Neil Moore
List and list operations (continue).
Defining Functions with DEFUN
Peter Seibel Practical Common Lisp Peter Seibel
Abstraction and Repetition
Functional Programming: Lisp
Lisp: Using Functions as Data
Lisp.
Presentation transcript:

A function (negatives L) that takes a list of numbers L and returns a list containing only negative numbers from L. A recursive example to remind you (defun negatives(L) ) (cond ( ) ) ( t )(negatives ) ( ) Go through the list recursively one element at a time On each loop, check if the first element of the list is < 0 stop when? Stopping condition(null L) Function definition recursive condition2 ‘() (rest L) If current element < 0, cons that element to recursive answer (cons ) Recursive condition1(< (first L) 0) When we get to the end of the list If it is not < 0, simply pass back the recursive answer (negatives )(first L)(rest L)

Examples of running this function > (negatives ‘(2 –1 3 -2) ) > (negatives ‘( (2 –1 (4 -2)) -3 (1 -4) ) ) What happens if we make this call? (-1 -2) ( -3 ) We only get negatives on the top level of the list; the negatives function does not go “down into” internal lists. (defun negatives(L) (cond ( (null L) ‘() ) ( (< (first L) 0 ) (cons (first L) (negatives (rest L))) ) ( t (negatives (rest L)) ) )) Why not? Because the function doesn’t check if the first element of L is a list itself.

We want to write a function (all_negatives L) that finds all the negative numbers in a list and in any sublists of that list. >(all_negatives ‘( (2 –1 (4 -2)) -3 (1 -4) ) ) ( – ) How should this function work? It should recurse along the list L, processing (first L) and adding to the results from (all_negatives (rest L)). What do we want the function to do with (first L)? If (first L) is a number:Check if it’s negative; if so, add to the (all_negatives (rest L)) result If (first L) is a list:Get all negatives in the list (first L) add to (all_negatives (rest L)) result How do we do this? By calling (all_negatives (first L))

Double recursion:two recursive calls To get all_negatives in a list L, we want to Get all_negatives in (first L) Get all_negatives in (rest L) if (first L) is a list itself normal recursion Join these two results together to give us all_negatives in the overall list An example (assume all_negatives function is working) (all_negatives ‘( (2 –1 (4 -2)) -3 (1 -4) ) ) L First Lrest L (all_negatives ‘(2 –1 (4 -2)) )(all_negatives ‘( -3 (1 -4)) ) ‘(-1 -2) ‘(-3 -4) (append ) ( ) We make 2 recursive calls: one to get all_negatives in the (first L) list, the other to get all_negatives in the (rest L) list. Each call will return a list of negatives as its answer: join them using append.

(all_negatives ) (defun all_negatives(L) ) (cond ( ) ) ( t ) (all_negatives ) ( ) If (first L) is number <0? If (first L) is a list? (null L)‘() (rest L) otherwise (cons ) (< (first L) 0) (all_negatives )(first L)(rest L) Writing the function Stopping condition? ( ) (append ) (listp (first L) ) (all_negatives )(first L)(rest L) Cons (first L) to result being built If (first L) is a list we do a recursive call to get all negatives in (first L) and join them to the result being built

Why double recursion is simple Most of the functions that we write are designed to operate on lists. But we already know the function operates on lists; we just change the code a bit so that it can call itself on internal lists. 15 >(total_sum ‘( 2 (2 1 (3)) 2 (1 (2 ( (1) 1))) ) We usually do a double recursion when we want to handle a list within a list. Another function that needs to be doubly recursive : total_sum Gets the total sum of all numbers in a list and its sublists

Another example: depth in a list 0 >(max_depth ‘( a cat and a dog ) ) 1 >(max_depth ‘( a cat and (a rat and a dog)) ) 3 >(max_depth ‘( a cat and (a rat (with green eyes (that glow) ) and a dog) ) ) The maximum depth of a list is +1 the maximum depth of the sublists in that list. If there are no sublists, the depth is 0 We can use the built-in function (max X Y) which takes two numbers and returns the biggest one.

(defun max_depth(L) ) (cond ( ) ) ( ) (max ) One possible soln for max_depth (max_depth ) ( t ) (max_depth ) (null L)‘() (rest L) (listp (first L) ) (max_depth )(first L)(+ 1 )(rest L)

How far ‘down’ do these functions go? One thing that people sometimes wonder about these “doubly recursive” functions is, what happens if there’s a list inside a list inside a list? do I have to add another line to my function to handle that? The answer is no. By providing recursive calls on the first element of a list and the rest of a list, we are making a function that can handle lists inside lists. When we call that function for the first element of a list, that function can handle any sublists of that first element. It will call itself for those sublists, and so will handle any sublists of those sublists, and any sublists of those, and so on, all the way down.