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.

Slides:



Advertisements
Similar presentations
C++ Programming:. Program Design Including
Advertisements

Introduction to Recursion and Recursive Algorithms
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)
Recursion (define tell-story (lambda () (print ‘’Once upon a time there was a mountain. ‘’) (print ‘’On the mountain, there was a temple. ‘’) (print ‘’In.
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.
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
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.
Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
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?
Classes, methods, and conditional statements We’re past the basics. These are the roots.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
29-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
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.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
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.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Conditionals and Recursion "To iterate is human, to recurse divine." - L. Peter Deutsch.
Clojure 3 Recursion, Higher-order-functions 27-Aug-15.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Common lisp A functional programming language. Useful URL:
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
RecursionRecursion Recursion You should be able to identify the base case(s) and the general case in a recursive definition To be able to write a recursive.
Recursion AP Computer Science A Mr. Langner By: Thomas Robbins.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
Recursion.
CS314 – Section 5 Recitation 10
Recursion 4-Jun-18.
Java 4/4/2017 Recursion.
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Recursion 12-Nov-18.
Recursion Chapter 11.
Recursion 2-Dec-18.
Recursion 2-Dec-18.
Recursion 29-Dec-18.
Modern Programming Languages Lecture 20 Fakhar Lodhi
Recursion, Higher-order-functions
Recursion 10-Apr-19.
Recursion 23-Apr-19.
Common Lisp II.
ITEC324 Principle of CS III
Presentation transcript:

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 ( (> N 1) (* N (factorial (- N 1)))) ( (= N 1) 1) )) Recursion: Doing loops in Lisp (Factorial N) is equal to the product of all numbers from N down to 1. (Factorial 5)=5*4*3*2*1 = 120 If the number is > 1 Do the multiplication Take one away from the number Repeat the operation with the smaller number There is no for; to repeat an operation we write a function that calls itself: a recursive function Notice that all the same operations get done; just in a different order.

Recursion in lisp A recursive function is a function which calls itself as a subroutine; as a helper to solve a problem A recursive function works as follows. When it is asked a question (given an argument), it 1)Checks if the argument is so simple it can give the answer straight away. If so it gives the answer and stops. 2) If the argument is too “big” to give an immediate answer it Computes a smaller version of the argument Calls itself as a helper to get an answer for the smaller arg Combines that answer with the left-behind part of the “big” argument, to get the overall answer to the original question

1) Check if the argument is so simple it can give an answer straight away. If so, give the answer & stop Calls itself as a helper to get an answer for that smaller argument (defun factorial(N) (cond ( (> N 1) (* N (factorial (- N 1)))) ( (= N 1) 1 ) ) ) Compute a smaller version of the argument Combines that answer with the left-behind part of the “big” argument, to get the overall answer to the original question Parts of the recursive function 2) If the argument is too big to give an immediate answer

1. Make a copy of the function and replace N by 2 (defun factorial(2) (cond ( (> 2 1) (* 2 (factorial (- 2 1)))) ( (= 2 1) 1) ) ) 2. Evaluate the body of the function (cond ( (> 2 1) (* 2 (factorial (- 2 1)))) ( (= 2 1) 1) ) 3a. Evaluate (- 3 1) (* 3 (factorial (- 3 1) ) ) 3b. Evaluate (factorial 2) (* 3 (factorial 2) ) 3c. Multiply by 3 (* 3 (factorial 2) ) 1. Make a copy of the function and replace N by 3 (defun factorial(3) (cond ( (> 3 1) (* 3 (factorial (- 3 1)))) ( (= 3 1) 1) ) ) 3a. Subtract 1 from 2 (* 2 (factorial (- 2 1))) 3b. Evaluate (factorial 1) (* 2 (factorial 1) ) 3c. Multiply by 2 (* 2 (factorial 1) ) 3. (> 2 1) is true (t); evaluate (* 2 (factorial (- 2 1))) 2. Evaluate the body of the function (cond ( (> 3 1) (* 3 (factorial (- 3 1)))) ( (= 3 1) 1) ) ) 3. (> 3 1) is true (t); evaluate (* 3 (factorial (- 3 1))) (defun factorial(N) (cond ( (> N 1) (* N (factorial (- N 1)))) ( (= N 1) 1 ) ) ) How does recursion work? > (factorial 3) Make a copy of the function and replace N by 1 (defun factorial(1) (cond ( (> 1 1) (* 2 (factorial (- 1 1)))) ( (= 1 1) 1) ) ) 2. Evaluate the body of the function (cond ( (> 1 1) (* 1 (factorial (- 1 1)))) ( (= 1 1) 1) ) ) 3. (= 1 1) is true (t); just return 1 as the answer ( t 1) Tracing a call to the function:

Every recursive function has A stopping condition; when this is true the answer can be returned directly with no more function calls In the recursive condition the function always calls itself with “smaller” or “simpler” arguments (defun factorial(N) (cond ( (> N 1) (* N (factorial (- N 1)))) ( (= N 1) 1 ) ) ) A recursive condition: when this is true the function must call itself to get an answer (going around the loop) Without both a stopping condition and smaller arguments, you get infinite recursion; a neverending loop. The answer is built up by taking the answer from the recursive call and doing something to it

Recursion with lists Lists are central in lisp To process a list (to find an element, or do something to all elements) we need to do some operation over and over: a loop We write a recursive function with lists as arguments. A simple task: count the number of elements in a list Suppose L is the list we are looking at. Our recursive function will use (first L) to select the current first element of the list and do something to it It will use (rest L) to get a smaller part of the list, and do the recursive call (the loop) with that smaller list.

count the number of elements in a list (+ 1 ) ( t ) (len ) This is going to be a loop where we consider each element of a list (count that element) when we’ve counted an element, go on to rest of the list stop when we get to the end of the list Stopping condition(null L)(cond ( ) ) Function definition (defun len(L) ) recursive condition (rest L) 0

Comparing two recursive functions (Defun len(L) (cond ( (null L) 0) ( t (+ 1 (len (rest L)))) )) (Defun factorial(N) (cond ( (> N 1) (* N (factorial (- N 1)))) ( (= N 1) 1 ) )) Stopping conditions? Recursive conditions? The t in len means “otherwise”. We could rewrite factorial to use t. (Defun factorial(N) (cond ( (= N 1) 1) ( t (* N (factorial (- N 1)))) ))