In this posting of we show yet another implementation of sumlist using an operator which will be new to many readers. While the do construct is common to many lisp dialects, particularly Scheme implementations, it is somewhat controversial as some people love it, and some people hate it. Some people find it elegant and expressive, and other find it terse and awkward. I'll present it here and let you decide for yourself.
Recall the implementation of sumlist_1a which accepts a list of numbers as its argument and returns the arithmetic of these numbers.
(defun sumlist_1a (numbers) (let ((sum 0)) (foreach number numbers sum = sum + number) sum))
Here is yet another implementation of the sumlist function which we've seen in the past several postings. This version of the function uses the (do ...) construct.
(defun sumlist_5a (numbers) (do ((remaining numbers (cdr remaining)) (sum_so_far 0 (plus sum_so_far (car remaining)))) ((null remaining) sum_so_far)))
Evolution of Lisp
An excerpt from Evolution of Lisp:
Examining the example
The sumlist_5a uses do to iterate two variables remaining and sum_so_far from their respective initial values to their final values, using respective formulas to update the variables to their next values.
As was mentioned above, the syntax of do is somewhat off-putting. There are lots of parentheses, and several interdependent components which you have to get right. The following is an itemization of the various parts of the (do ...) loop.
(do ((remaining numbers (cdr remaining)) (sum_so_far 0 (plus sum_so_far (car remaining)))) ((null remaining) sum_so_far))
remaining = numbers
sum_so_far = 0
remaining = (cdr remaining)
sum_so_far = (plus sum_so_far (car remaining))
(do ((remaining numbers (cdr remaining)) (sum_so_far 0 (plus sum_so_far (car remaining)))) ((null remaining) sum_so_far) (println remaining) (printf "partial sum = %L\n" sum_so_far))
One way to think of the SKILL do is as a mixture or generalization of several iteration constructs.
Do as for
The following is like a (for ...) loop.
(do ((i 0 i+1)) (i==100 t) ; return t because for always returns t (println i))
(for i 0 100 (println i))
Do as foreach
The following is like a (foreach ...) loop.
(let ((some_list '(a b c d))) (do ((sub some_list (cdr sub)) (item (car some_list) (cadr sub))) ((null sub) some_list) ; return some_list because foreach returns the list it iterated over (println item)))
(let ((some_list '(a b c d))) (foreach i some_list (println i)))
Do as a mixture of for and foreach
The (foreach ...) iterates successively through a given list of items. The (for ...) loop iterates successively through a range of integers, given the lower and upper bounds of the range. If you want to iterate one variable through a given list while simultaneously iterating another variable through a range of integers, you can use the (do ...) loop.
(let ((some_list '(a b c d ...))) (do ((index 0 (add1 index)) (sub some_list (cdr sub)) (item (car some_list) (cadr sub))) ((null sub) t) (printf "The %d'th of the list is %L\n" index item)))
The 0'th of the list is aThe 1'th of the list is bThe 2'th of the list is cThe 3'th of the list is dThe 4'th of the list is eThe 5'th of the list is fThe 6'th of the list is g
The SKILL do can be tricky to use. It allows the programmer control of several parallel iteration variables, all of which are potentially incremented according to different rules. You may also explicitly determine the return value, which is fixed and useless for other iteration constructs such as (for ...). You may also specify a series of expressions to evaluate when the iteration finishes while the iteration variables are still in scope holding their final values.
Hopefully you can use the steps above as sort of a cookbook.
More to come
I recently found this reader question which was lost in my inbox.
I discovered the "do" syntax on your latest post "skill for the skilled (part5)"
This is very interesting. But I discovered that unfortunatly, skill interpretor that is embedded with Virtuoso does not support it.
Is there any plan to support it ? Did i make a mistake trying it ?
Thanks for your feedback
Glad you found the "do loop" interesting. You'll find that it does not work in traditional SKILL, you have to use SKILL++. So you'll need to name your file with a .ils extension, or you'll need to wrap the function definition with (inScheme ...).
Here is what happens if you try to use do directly in the CIWindow.
CIW> (inScheme (do () (t)))
CIW> (do () (t))
*Error* do: not supported in SKILL - do(nil (t))
<<< Stack Trace >>>