6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology 6.001 Notes: Section 3.2 Slide 3.2.1 A less trivial procedure: factorial Now let's look at a little less trivial example. Suppose I want to Compute n factorial, defined as nl =n(n-1)(n-2)(n-3),1 compute factorial of n, which is defined as the product of n n-1 and so on down to 1 note that i am assuming that n is integer and is greater than or equal to 1 A less trivial procedure: factorial Slide 3.2.2 So how do i create a procedure to compute factorial of n? Wel . Notice that nl=n"[(n-1)(n-2).=n"(n-1) ifn>1 I need to think careful choice in de procedure will impact the evolution of the process when I use One way to look at factorial is to group the computation into multiply ing n by all the other multiplications, but I can recognize that this latter computation is just factorial of n-1 So notice what this does, it reduces a computation to a simpler operation, multiplication, and a simpler version of the same I problem, in this case factorial of a smaller number. So I have reduced one version of a problem to a simpler version same problem, plus some simple operations. This is a common pattern that we are going to use a lot in designing procedures Slide 3.2.3 A less trivial procedure: factorial In fact, given this common pattern, we can write a procedure to Compute n factorial, defined as n! =n(n-1)(n-2)(n-3),1 capture this idea. Here it is. The first part says to give the name Notice that n!=n (n-1)(n-2) l=n*(n-1)!ifn>1 fact to the procedure created by the lambda, which has a single|(define fact argument n and a particular body. Now, what does the lambda lambda (n) say to do? It has two different parts, which we need to look at carefully n(fact(-n1)))))) 41∞036.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. 6.001 Notes: Section 3.2 Slide 3.2.1 Now let's look at a little less trivial example. Suppose I want to compute factorial of n, which is defined as the product of n by n-1 and so on down to 1. Note that I am assuming that n is an integer and is greater than or equal to 1. Slide 3.2.2 So how do I create a procedure to compute factorial of n? Well I need to think carefully here, since my choice in designing this procedure will impact the evolution of the process when I use it. One way to look at factorial is to group the computation into multiplying n by all the other multiplications, but I can recognize that this latter computation is just factorial of n-1. So notice what this does, it reduces a computation to a simpler operation, multiplication, and a simpler version of the same problem, in this case factorial of a smaller number. So I have reduced one version of a problem to a simpler version of the same problem, plus some simple operations. This is a very common pattern that we are going to use a lot in designing procedures. Slide 3.2.3 In fact, given this common pattern, we can write a procedure to capture this idea. Here it is. The first part says to give the name fact to the procedure created by the lambda, which has a single argument n and a particular body. Now, what does the lambda say to do? It has two different parts, which we need to look at carefully