Repetition and iteration constructs cause something to be done a given number of times or to be done to each element in a list of elements. Repetition control structures are said to be one kind of control strategy, which uses conditions to determine how many times to repeat execution of an action.
In AutoCad program, the command array can be used to make copies of a drawing object a certain number of times in either rectangular or polar configuration. This is an example of repetition- a command to place copies of a drawing element repeats until specified number of times. In the following are explained various forms in which you can express such control structures.
In this form, the condition specified is checked at the top of the loop. If it is true, all the statements (actions) specified in the body of the loop are executed. This process will stop only when the condition somehow becomes false, otherwise you get a loop that will go on forever - an infinite loop! Usually, one of the actions in body of the loop does something that causes the condition to evaluate to false.
Examples using numbers.
This is quite similar to the while construct except that the number of times the loop should execute is known in advance. This is a safer form of iteration in which it is easy to avoid infinite loops.
Programs are made up of nested procedures, where one procedure calls another to do part of the work and uses results returned by it. Writing programs as a collection of small functions- also known as modular programming, makes it easier to debug programs. There is another form of procedure that calls upon itself, accumulating results as it goes along - this is known as recursion.
Example: Compute factorial of number n.
Simulation of factorial function for n = 3
Recursive functions can get quite complicated and hard to debug. First determine the boundary conditions- when the function cannot call itself anymore, what result it should return, and how to accumulate results.
One way of debugging recursive functions (in fact, all functions in LISP) is by using the built-in predicate trace. You can set a trace on any user defined function and watch it execute. You will be shown actual values of arguments passed to that function when it is invoked, and the value it returns when that function exits.
For example, to trace above function for computing factorials, you should type:
And then invoke the function with some argument:
This exercise gives you an opportunity to use both iteration and recursion control structures in writing a program.
Your program should prompt a user for width (h_width), number of vertical divisions (n) and height (hgt1). On each division at the specified height, it should generate a design as shown below. To generate elements on the top, use two more parameters: hgt2 which specifies the height, and offset which specifies distance between successive divisions in the top element. Note that each element consists of 7 replicas of itself.
Some helpful hints to get you started. Both the bottom and top portions of the design can be generated using any iterative structure. For detailed generation of elements on top, you can use recursive structure. You can define more parameters if needed in addition to the ones given above. Develop your program in a modular fashion so that it is easier to debug and test. Put it all together and submit a program file saved as 07_name.lsp. Copy your program file into directory /homes2/prog/abgabe.
Prog Content Vorwort ..1.. ..2.. ..3.. ..4.. ..5.. ..6.. ..7.. ..8.. ..9.. ..10.. ..11.. ..12.. ..13.. Appendix