Alternative to do*?

Discussion of Common Lisp
Post Reply
nessa203
Posts: 1
Joined: Mon Jan 10, 2011 5:47 am

Alternative to do*?

Post by nessa203 » Mon Jan 10, 2011 5:53 am

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!

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Alternative to do*?

Post by ramarren » Mon Jan 10, 2011 7:14 am

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.

Warren Wilkinson
Posts: 117
Joined: Tue Aug 10, 2010 11:24 pm
Location: Calgary, Alberta
Contact:

Re: Alternative to do*?

Post by Warren Wilkinson » Mon Jan 10, 2011 12:13 pm

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))
Need an online wiki database? My Lisp startup http://www.formlis.com combines a wiki with forms and reports.

Post Reply