Page 1 of 1

Alternative to do*?

Posted: Mon Jan 10, 2011 5:53 am
by nessa203
I'm basically writing a loop which i want to execute one-after-the-other rather than in parallel which is what 'do' does (if i'm understanding correctly!), I tried using do* but LispWorks threw an error at me - "Error: Unknown LOOP keyword in (...". So i'm a little confused.. :?:

Here is my code:

Code: Select all

(defun simplify (expanded-list simplified-list)
    (setq compared-value (car expanded-list))
    
    (loop for term in (cdr expanded-list) do
          (cond
           ((null term) 
            (progn (print "null") (return compared-value)))
           ((and (atom compared-value) (atom term))
            (progn
              (print "both numbers..")
              (print compared-value)
              (print term)
              (print (+ compared-value term))
              (print simplified-list)
              (print (cddr expanded-list))
              (return (list "newlist" 
                    (list (+ compared-value term) simplified-list (cddr expanded-list))))
              (print newlist)))
           ((and (listp compared-value) (listp term))
              (if (equalp (cdr compared-value) (cdr term))
                  (list "newlist" (list 
                                   (cons 
                                    (+ (car compared-value) (car term)) 
                                    (cdr term))
                                   simplified-list
                                   (cddr expanded-list)))))
           (t (list "newlist"
                    (list (cons compared-value simplified-list (cddr expanded-list)))))))
    (print "newlist:")
    (print newlist)
    (if (null (cddr expanded-list))
        newlist
      (simplify (cdr expanded-list) newlist)))
Any help would be much appreciated!

Re: Alternative to do*?

Posted: Mon Jan 10, 2011 7:14 am
by ramarren
It seems you are not aware that you are setting a global variable in the '(setq compared-value (car expanded-list))' line. And of the difference between DO macro, and the DO keyword of the LOOP syntax. You should probably (re)read an introductory text like Practical Common Lisp.

Re: Alternative to do*?

Posted: Mon Jan 10, 2011 12:13 pm
by Warren Wilkinson
loop is a mini language within lisp, like format.

Code: Select all

(loop for a in '(1 3 5)
    do (format t "~%Value: ~a" a)
    do (format t "~%Value (again): ~a" a)
   do  (format t "~%One last time: ~a" a))
Will show that those do's run sequentially.

The confusion probably stems because Lisp (not the loop language) also has a totally different construct called do (and a varient do*) that also make simple loops, they work like this:

Code: Select all


(do ((a 10 (1- a)))
      ((zerop a))
  (format t "~%I have: ~a" a)
  (format t "~%Once again: ~a" a)
  (format t "~%Last Time: ~a" a))