Page 81 - DCAP310_INTRODUCTION_TO_ARTIFICIAL_INTELLIGENCE_AND_EXPERT_SYSTEMS
P. 81

Unit 5: Artificial Intelligence Programming Language




          Inputs are variables that are created and filled with values when a procedure is invoked. The  Notes
          names of the variables, are given on the procedure’s title line. These identifiers can be used in
          the instructions which make up the body of the procedure. Inputs are also called local variables –
          they are local to the procedure in which they are defined.
          Just to refresh your memory, given:
             to box:size
             repeat 4 [ forward:size right 90 ]
               end
             box 100
          The first three lines are the definition of the procedure box, which includes an input, :size. A
          value is placed in the variable (think container) named size when the procedure box is invoked.
          The last line in this example shows an invocation of box. In this case, 100 is the value that’s being
          supplied for the: size variable.

          Self Assessment

          State whether the following statements are true or false:
          7.   Variables in programming languages are simply containers which hold values.
          8.   Outputs are variables that are created and filled with values when a procedure is invoked.

          9.   Inputs are also called local variables.
          5.4 Iteration and Recursion


          The fact is that recursion is rarely the most efficient approach to solving a problem, and iteration
          is almost always more efficient. This is because there is usually more overhead associated with
          making recursive calls due to the fact that the call stack is so heavily used during recursion. This
          means that many computer programming languages will spend more time maintaining the call
          stack then they will actually performing the necessary calculations. Generally speaking, yes it
          does. This is because of the extensive use of the call stack. Recursion is generally used because of
          the fact that it is simpler to implement, and it is usually more ‘elegant’ than iterative solutions.
          Remember that anything that’s done in recursion can also be done iteratively, but with recursion
          there is generally a performance drawback. But, depending on the problem that you are trying
          to solve, that performance drawback can be very insignificant – in which case it makes sense to
          use recursion. With recursion, you also get the added benefit that other programmers can more
          easily understand your code – which is always a good thing to have.

          Example of Recursion in Java – the Factorial

          We take a simple example of recursion to best illustrate how it works: the factorial is probably
          the most commonly used example. What is a factorial? Well, any number written like this: “x!”
          is said to be “the factorial of x”. A factorial of a number “x” is just the product of all integers
          between 1 and x. So, if x is equal to the number 5, then the factorial of x would be 5*4*3*2*1,
          which equals 120. We could also say that the factorial of is equal to 5 multiplied by the factorial
          of 4, which would be 5 * 4!, or 5*4*3*2*1 So, the factorial of any number “x” could also be defined
          as:
             x! = x * (x - 1)!

          And, something else that is important to know is the fact that the factorial of 0 is equal to 1 as is
          the factorial of 1.
             0! = 1! = 1



                                           LOVELY PROFESSIONAL UNIVERSITY                                   75
   76   77   78   79   80   81   82   83   84   85   86