Question
Examples: > ;; Iterate test 1 - simple > (setf n 5) 5 > (iterate i 1 (+ n 2) 2 (print (list 'one i
Examples:
> ;; Iterate test 1 - simple
> (setf n 5)
5
> (iterate i 1 (+ n 2) 2
(print (list 'one i (+ n 2) 2) )
)
(ONE 1 7 2)
(ONE 3 7 2)
(ONE 5 7 2)
(ONE 7 7 2)
9
> ;; Iterate test 2 - changing ctrl variable
> (setf n 5)
5
> (iterate i 1 (+ n 2) 2
(print (list 'two i (+ n 2) 2))
(+= i 1)
)
(TWO 1 7 2)
(TWO 4 7 2)
(TWO 7 7 2)
10
2. Code the macro, ITERATE, which is based on the following:
(iterate controlVariable beginValueExpr endValueExpr incrExpr bodyexpr1 bodyexpr2 ... bodyexprN)
iterate is passed a controlVariable which is used to count for beginValueExpr to endValueExpr (inclusive) by the specified increment incrExpr.
For each iteration, it evaluates each of the one or more body expressions.
Since beginValueExpr, endValueExpr, and incrExpr are expressions, they must be evaluated.
The endValueExpr is evaluated on every iteration. This means a use of the macro can alter the termination condition; it can also change the value of the controlVariable.
The incrExpr is evaluated before processing the rest of the macro. This means the code within the user's use of the macro cannot alter the effective increment.
The functional value of iterate will always be the final value of the controlVariable.
The execution should fall out of the loop when the value of controlVariable > the value of endValueExpr.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started