Understanding Common Lisp do macro syntax
(do ((n 0 (1+ n)) ;declares n, initially 0, n+1 each subsequent iteration)
(cur 0 next) ;declares cur, initially 0, then old value of next
(next 1 (+ cur next))) ;declares next, initially 1, then the sum of (the old) cur and next
((= 10 n) ;end condition (ends when n = 10)
cur) ; return value
;empty body
)
translating into c-like code
for(n=0, cur=0, next=1 ;
!(n == 10) ;
n=old_n+1, cur=old_next, next = old_cur + old_next)
{
//do nothing
old_n = n;
old_cur = cur;
old_next = next;
}
return cur;
incidentally you should be able to see that this code returns the 10th Fibonacci number
Optional EBNF/formal syntax:
The syntax according to the Hyperspec is:
(do ({var | (var [init-form [step-form]])}*)
(end-test-form result-form*)
declaration*
{tag | statement}*)
Understanding this requires knowledge of EBNF and big chunks of the Hyperspec
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).