(do ((n 0 (1+ n))
(cur 0 next)
(next 1 (+ cur next)))
((= 10 n) cur)))
This is an example from Lisp textbook about keyword "do"
the "do" basic template is:
(do (variable-definitions*)
(end-test-form result-form*)
statement*)
But, for this example, it's not clear to me which part is which. And also, what do te middle 2 lines do?
Thank you!
Common Lisp doesn't support macros so every Lisp programmer can create their own variants of standard control constructs any more than C supports functions so every C programmer can write trivial variants of the functions in the C standard library.
The do construct is also used for performing iteration using LISP. It provides a structured form of iteration. The initial values of each variable is evaluated and bound to the respective variable.
The Common Lisp macro facility allows the user to define arbitrary functions that convert certain Lisp forms into different forms before evaluating or compiling them. This is done at the expression level, not at the character-string level as in most other languages.
Your good indentation clearly shows which part is which:
(do ((n 0 (1+ n))
^(cur 0 next)
|(next 1 (+ cur next)))
|
+-- first argument of do
((= 10 n) cur)))
^
|
+-- start of second argument of do
Look, they line up nicely, and the inner material is indented:
((n 0 (1+ n))
(cur 0 next)
(next 1 (+ cur next)))
^
|
+- inner material of argument: three forms which are
indented by 1 character and aligned together.
Your do
doesn't have a third argument there: there is no body of statements (empty loop).
Sometimes it can help to 1. annotate the forms with comments and 2. print the current values in the body like so:
(do
;; varlist
((n 0 (1+ n))
(cur 0 next)
(next 1 (+ cur next)))
;; endlist
((= 10 n) cur)
;; body
(print (list n cur next)))
This prints
(0 0 1)
(1 1 1)
(2 1 2)
(3 2 3)
(4 3 5)
(5 5 8)
(6 8 13)
(7 13 21)
(8 21 34)
(9 34 55)
55
which should clear matters up. @_@
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With