utils.lisp 19.4 KB
Newer Older
1
(defpackage :wisp.util (:use :cl :cl-interpol); :it.bese.fiveam)
hayeah's avatar
hayeah committed
2
	    (:export :with-gensyms
3
;;		     :while
hayeah's avatar
hayeah committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
		     :until
		     
		     :fn
		     :mklst

		     :cat-syms
		     :strcat
		     :symcat ;; renamed cat-syms
		     
		     :filter
		     :mappend
		     :group
		     :mvbind
		     :mvsetq 
		     :dbind

		     ;;anaphoric macros
		     :it
		     :self
		     :aif 
		     :awhen
		     :awhile
		     :acond
		     :aand
28 29 30
		     :afn

		     :split-if))
hayeah's avatar
hayeah committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

(in-package :wisp.util)


(defmacro while (condition &body body)
  (let ( (tag (gensym "LABEL")) )
    `(tagbody ,tag (when ,condition ,@body (go ,tag)))))

(defmacro until (condition &body body)
  (let ( (tag (gensym "LABEL")) )
    `(tagbody ,tag (unless ,condition ,@body (go ,tag)))))


(defun group (source n)
  (if (zerop n) (error "zero length"))
  (labels ((rec (source acc)
	     (let ((rest (nthcdr n source)))
	       (if (consp rest)
		   (rec rest (cons (subseq source 0 n) acc))
		   (nreverse (cons source acc))))))
    (if source (rec source nil) nil)))


(defmacro abbrev (short long)
  `(defmacro ,short (&rest args) `(,',long ,@args)))

(defmacro abbrevs (&rest names)
  `(progn ,@(mapcar #'(lambda (pair) `(abbrev ,@pair))
		    (group names 2))))

(abbrevs mvbind multiple-value-bind
	 mvsetq multiple-value-setq
63 64 65 66 67 68 69
	 dbind destructuring-bind
	 -> setf
	 --> set
	 
;; 	 <- getf
;; 	 <-- get
	 )
hayeah's avatar
hayeah committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784

(defmacro fn (args &body body)
  (cond
   ((null args) `#'(lambda () ,@body))
   ((atom args) `#'(lambda (&rest ,args) ,@body))
   ((cdr (last args))
     (let* ((args (copy-list args))
             (last (last args)))
       (setf (cdr last) (list '&rest (cdr last)))
       `#'(lambda ,args ,@body)))
   (t `#'(lambda ,args ,@body))))


(defun mklst (obj)
  (if (listp obj) obj (list obj)))

(defmacro with-gensyms (syms &body body)
  `(let ,(mapcar #'(lambda (s)
		     `(,s (gensym)))
		 syms)
    ,@body))

(defun cat-syms (&rest los)
  (values (intern (apply #'concatenate (cons 'string (mapcar #'symbol-name los))))))

(defun strcat (&rest strings)
  (apply 'concatenate 'string strings))

(defmacro with-char-collector (var &rest body)
  (let ( (svar (gensym "S")) )
    `(let ( (,svar (make-array 0 :element-type 'base-character
                               :fill-pointer t :adjustable t)) )
       (flet ( (,var (c) (vector-push-extend c ,svar)) )
         ,@body
         ,svar))))

(defun symcat (&rest symbols)
  (intern (apply 'strcat (mapcar 'symbol-name symbols))))




;; (defun pp (&rest args)
;;   (pprint args) 
;;   (values))

(defun filter (fn lst)
  (let ((acc nil))
    (dolist (x lst)
      (let ((val (funcall fn x)))
	(if val (push val acc))))
    (nreverse acc)))

(defun mappend (fn &rest lsts)
  "Non-destructive mapcan"
  (apply #'append (apply #'mapcar fn lsts)))



;; doesn't support fancy lambda list with &optional, &rest, etc.

;; (defmacro dbind (pat seq &body body)
;;   "Destructuring bind for type sequence. (Ch. 19 Onlisp)"
;;   (let ((gseq (gensym)))
;;     `(let ((,gseq ,seq))
;;       ,(dbind-ex (destruc pat gseq #'atom) body))))

;; (defun destruc (pat seq &optional (atom? #'atom) (n 0))
;;   (if (null pat)
;;       nil
;;       (let ((rest (cond ((funcall atom? pat) pat)
;; 			((eq (car pat) '&rest) (cadr pat))
;; 			((eq (car pat) '&body) (cadr pat))
;; 			(t nil))))
;; 	(if rest
;; 	    `((,rest (subseq ,seq ,n)))
;; 	    (let ((p (car pat))
;; 		  (rec (destruc (cdr pat) seq atom? (1+ n))))
;; 	      (if (funcall atom? p)
;; 		  (cons `(,p (elt ,seq ,n))
;; 			rec)
;; 		  (let ((var (gensym)))
;; 		    (cons (cons `(,var (elt ,seq ,n))
;; 				(destruc p var atom?))
;; 			  rec))))))))

;; (defun dbind-ex (binds body)
;;   (if (null binds)
;;       `(progn ,@body)
;;       `(let ,(mapcar #'(lambda (b)
;; 			 (if (consp (car b))
;; 			     (car b)
;; 			     b))
;; 		     binds)
;; 	,(dbind-ex (mapcan #'(lambda (b)
;; 			       (if (consp (car b))
;; 				   (cdr b)))
;; 			   binds)
;; 		   body))))

;; (defmacro dlet (binds &body body)
;;   (cond ((null binds) `(progn ,@body))
;; 	((null (cdr binds)) (error (format nil "dlet syntax error: unmatched binding pair in:~&~s" binds)))
;; 	((consp (car binds)) `(dbind ,(car binds) ,(cadr binds) (dlet ,(cddr binds)  ,@body)))
;; 	(t `(let ((,(car binds) ,(cadr binds))) (dlet ,(cddr binds) ,@body)))))
	



;; From onlisp

;; Anaphoric Macros

(defmacro aif (test-form then-form &optional else-form)
  `(let ((it ,test-form))
     (if it ,then-form ,else-form)))

(defmacro awhen (test-form &body body)
  `(aif ,test-form
	(progn ,@body)))

(defmacro awhile (expr &body body)
  `(do ((it ,expr ,expr))
       ((not it))
     ,@body))

(defmacro aand (&rest args)
  (cond ((null args) t)
	((null (cdr args)) (car args))
	(t `(aif ,(car args) (aand ,@(cdr args))))))

(defmacro acond (&rest clauses)
  (if (null clauses)
      nil
      (let ((cl1 (car clauses))
	    (sym (gensym)))
	`(let ((,sym ,(car cl1))) ;; bind over the narrowest scope possible.
	  (if ,sym
	      (let ((it ,sym)) ,@(cdr cl1))
	      (acond ,@(cdr clauses)))))))  ;; otherwise would appear in the test expression

(defmacro afn (parms &body body)
  `(labels ((self ,parms ,@body))
     #'self)) ;; funny



;; ;; When there are two returned values, bind the first, and also test the second.
;; ;; ;; Useful for gethash, for example
;; ;; (defmacro aif2 (test &optional then else)
;; ;;   (let ((win (gensym)))
;; ;;     `(multiple-value-bind (it ,win) ,test
;; ;;        (if (or it ,win) ,then ,else))))

;; ;; (defmacro awhen2 (test &body body)
;; ;;   `(aif2 ,test
;; ;; 	 (progn ,@body)))

;; ;; (defmacro awhile2 (test &body body)
;; ;;   (let ((flag (gensym)))
;; ;;     `(let ((,flag t))
;; ;;        (while ,flag
;; ;; 	 (aif2 ,test
;; ;; 	       (progn ,@body)
;; ;; 	       (setq ,flag nil))))))

;; ;; (defmacro acond2 (&rest clauses)
;; ;;   (if (null clauses)
;; ;;       nil
;; ;;     (let ((cl1 (car clauses))
;; ;; 	  (val (gensym))
;; ;; 	  (win (gensym)))
;; ;;       `(multiple-value-bind (,val ,win) ,(car cl1)
;; ;; 	 (if (or ,val ,win)
;; ;; 	     (let ((it ,val)) ,@(cdr cl1))
;; ;; 	   (acond2 ,@(cdr clauses)))))))



;; Ron Garret:
;; (defmacro acond (&rest clauses)
;;   (if (null clauses)
;;     nil
;;     `(aif ,(caar clauses)
;;        (progn ,@(cdar clauses))
;;        (acond ,@(cdr clauses))))) ;; captures 'it' in the test expression










(proclaim '(inline last1 single append1 conc1 mklist))

(defun last1 (lst)
  (car (last lst)))

(defun single (lst)
  (and (consp lst) (not (cdr lst))))

(defun append1 (lst obj)
  (append lst (list obj)))

(defun conc1 (lst obj)
  (nconc lst (list obj)))

(defun mklist (obj)
  (if (listp obj) obj (list obj)))

(defun longer (x y)
  (labels ((compare (x y)
	     (and (consp x)
		  (or (null y)
		      (compare (cdr x) (cdr y))))))
    (if (and (listp x) (listp y))
	(compare x y)
	(> (length x) (length y)))))





(defun flatten (x)
  (labels ((rec (x acc)
	     (cond ((null x) acc)
		   ((atom x) (cons x acc))
		   (t (rec (car x) (rec (cdr x) acc))))))
    (rec x nil)))

(defun prune (test tree)
  (labels ((rec (tree acc)
	     (cond ((null tree) (nreverse acc))
		   ((consp (car tree))
		    (rec (cdr tree)
			 (cons (rec (car tree) nil) acc)))
		   (t (rec (cdr tree)
			   (if (funcall test (car tree))
			       acc
			       (cons (car tree) acc)))))))
    (rec tree nil)))

					; return both the item and the test-result.

(defun find2 (fn lst)
  (if (null lst)
      nil
      (let ((val (funcall fn (car lst))))
	(if val
	    (values (car lst) val)
	    (find2 fn (cdr lst))))))
					; 
;; 
(defun before (x y lst &key (test #'eql))
  "Test if x occurs before y in lst. Returns (x ...) if y doesn't occur at all (i.e. equivalent to #'member's result)."
  (and lst
       (let ((first (car lst)))
	 (cond ((funcall test y first) nil)
	       ((funcall test x first) lst)
	       (t (before x y (cdr lst) :test test))))))


(defun after (x y lst &key (test #'eql))
  (let ((rest (before y x lst :test test)))
    (and rest (member x rest :test test))))

(defun duplicate (obj lst &key (test #'eql))
  (member obj (cdr (member obj lst :test test))
	  :test test))

(defun split-if (fn lst)
  (let ((acc nil))
    (do ((src lst (cdr src)))
	((or (null src) (funcall fn (car src)))
	 (values (nreverse acc) src))
      (push (car src) acc))))

(defun most (fn lst)
  "returns the highest scored element in lst calculated with fn"
  (if (null lst)
      (values nil nil)
      (let* ((wins (car lst))
	     (max (funcall fn wins)))
	(dolist (obj (cdr lst))
	  (let ((score (funcall fn obj)))
	    (when (> score max)
	      (setq wins obj
		    max score))))
	(values wins max))))


(defun best (fn lst)
  "Returns the winner by comparing all elements with the 2-place predicate. Like #'car of #'sort"
  (if (null lst)
      nil
      (let ((wins (car lst)))
	(dolist (obj (cdr lst))
	  (if (funcall fn obj wins)
	      (setq wins obj)))
	wins)))


(defun mostn (fn lst)
  "like most, except it collects all the element with the same highscore"
  (if (null lst)
      (values nil nil)
      (let ((result (list (car lst)))
	    (max (funcall fn (car lst))))
	(dolist (obj (cdr lst))
	  (let ((score (funcall fn obj)))
	    (cond ((> score max)
		   (setq max score
			 result (list obj)))
		  ((= score max)
		   (push obj result)))))
	(values (nreverse result) max))))


(defun map0-n (fn n) 
  "map over a generated list of (0 ... n) inclusive"
  (mapa-b fn 0 n))

(defun map1-n (fn n)
  "map over a generated list of (1 ... n) inclusive"
  (mapa-b fn 1 n))

(defun mapa-b (fn a b &optional (step 1))
  "map over generated list of (a ... b) inclusive"
  (do ((i a (+ i step))
       (result nil))
      ((> i b) (nreverse result))
    (push (funcall fn i) result)))

(defun map-> (fn start test-fn succ-fn)
  "Map over a generated list"
  (do ((i start (funcall succ-fn i))
       (result nil))
      ((funcall test-fn i) (nreverse result))
    (push (funcall fn i) result)))



;; mapcan may be thought of as the above, but applying #'nconc

(defun mapcars (fn &rest lsts)
  (let ((result nil))
    (dolist (lst lsts)
      (dolist (obj lst)
	(push (funcall fn obj) result)))
    (nreverse result)))

(defun rmapcar (fn &rest args)
  "recursive mapcar"
  (if (some #'atom args)
      (apply fn args)
      (apply #'mapcar
	     #'(lambda (&rest args)
		 (apply #'rmapcar fn args))
	     args)))

(defmacro when-bind* (binds &body body)
  (if (null binds)
      `(progn ,@body)
      `(let (,(car binds))
	(if ,(caar binds)
	    (when-bind* ,(cdr binds) ,@body)))))

(defmacro condlet (clauses &body body)
  (let ((bodfn (gensym))
        (vars (mapcar #'(lambda (v) (cons v (gensym)))
                      (remove-duplicates
                       (mapcar #'car
                               (mappend #'cdr clauses))))))
    `(labels ((,bodfn ,(mapcar #'car vars)
                      ,@body))
       (cond ,@(mapcar #'(lambda (cl)
                            (condlet-clause vars cl bodfn))
                        clauses)))))

(defun condlet-clause (vars cl bodfn)
  `(,(car cl) (let ,(mapcar #'cdr vars)
                (let ,(condlet-binds vars cl)
                  (,bodfn ,@(mapcar #'cdr vars))))))

(defun condlet-binds (vars cl)
  (mapcar #'(lambda (bindform)
              (if (consp bindform)
                  (cons (cdr (assoc (car bindform) vars))
                        (cdr bindform))))
          (cdr cl)))

(defmacro condlet* (clauses &body body)
  (let ((bodfn (gensym))
	(vars (mapcar #'(lambda (v) (cons v (gensym)))
		      (remove-duplicates
		       (mapcar #'car
			       (mappend #'cdr clauses))))))
    `(labels ((,bodfn ,(mapcar #'car vars)
	       ,@body))
      (cond ,@(mapcar #'(lambda (cl)
			  (condlet*-clause vars cl bodfn))
		      clauses)))))

(defun condlet*-clause (vars cl bodfn)
  `(,(car cl) (let* ,(mapcar #'cdr vars)
		(let* ,(condlet*-binds vars cl)
		  (,bodfn ,@(mapcar #'cdr vars))))))

(defun condlet*-binds (vars cl)
  (mapcar #'(lambda (bindform)
	      (if (consp bindform)
		  (cons (cdr (assoc (car bindform) vars))
			(cdr bindform))))
	  (cdr cl)))

;; I don't get this
;; (defmacro if3 (test t-case nil-case ?-case)
;;   `(case ,test
;;      ((nil) ,nil-case)
;;      (? ,?-case)
;;      (t ,t-case)))


(defmacro nif (expr pos zero neg)
  "numeric (+,0,-) 3-ways if"
  (let ((g (gensym)))
    `(let ((,g ,expr))
      (cond ((plusp ,g) ,pos)
	    ((zerop ,g) ,zero)
	    (t ,neg)))))

;; More efficient way to test set membership, when elements are not in a list yet.
;; (in x 2 3 4) == (member x (list 2 3 4))

(defmacro in (obj &rest choices)
  (let ((insym (gensym)))
    `(let ((,insym ,obj))
      (or ,@(mapcar #'(lambda (c) `(eql ,insym ,c))
		    choices)))))

;; with the set elements quoted
;; (inq x + - $) == (in x '+ '- '$)
(defmacro inq (obj &rest args)
  `(in ,obj ,@(mapcar #'(lambda (a)
			  `',a)
		      args)))

;; (in-if #'(lambda (y) (equal x y)) a b) == (member x (list a b) :test #'equal)
;; (in-if #'oddp a b) == (some #'oddp a b)
(defmacro in-if (fn &rest choices)
  (let ((fnsym (gensym)))
    `(let ((,fnsym ,fn))
      (or ,@(mapcar #'(lambda (c)
			`(funcall ,fnsym ,c))
		    choices)))))

;; I can't decide how much these "in"s improves overall efficiency.
;; I mean, how often would I have 1 thousand variables not in list, and want to test membership?
;; In normal circumstances, it seems that the gain is fairly marginal. But then, it adds up.
;;;; so if i have on my hand a list, then it doesn't make sense to use these macros.


;; Like case, but can have expression as keys. Keys must be in list, except 't' and 'otherwise'
(defmacro >case (expr &rest clauses)
  (let ((g (gensym)))
    `(let ((,g ,expr))
      (cond ,@(mapcar #'(lambda (cl) (>casex g cl))
		      clauses)))))

(defun >casex (g cl)
  (let ((key (car cl)) (rest (cdr cl)))
    (cond ((consp key) `((in ,g ,@key) ,@rest))
	  ((inq key t otherwise) `(t ,@rest))
	  (t (error "bad >case clause")))))

;; (>case 7
;;        ((1 (+ 3 4)) 1)
;;        ((3) 2)
;;        (((+ 1 1) 4) 3)
;;        ((nil) 5)
;;        (t 4))



;; ;;;; This is excellent!
;; (defmacro do-tuples/o (parms source &body body)
;;   (if parms
;;       (let ((src (gensym)))
;; 	`(prog ((,src ,source))
;; 	       (mapc #'(lambda ,parms ,@body)
;; 		     ,@(map0-n #'(lambda (n)
;; 				    `(nthcdr ,n ,src))
;; 				(1- (length parms))))))))

;; (do-tuples/o (x y z) '(1 2 3 4)
;; 	     (princ (list x y z)))


;; Alternate definition with loop macro
(defmacro do-tuples/o (parms source &body body)
  (if parms
      (let ((src (gensym)))
	`(prog ((,src ,source))
	  (mapc #'(lambda ,parms ,@body)
	   ,@(loop for n from 0 to (1- (length parms))
		   collect `(nthcdr ,n ,src)))))))

(defmacro do-tuples/c (parms source &body body)
  (if parms
      (with-gensyms (src rest bodfn)
	(let ((len (length parms)))
	  `(let ((,src ,source))
	    (when (nthcdr ,(1- len) ,src)
	      (labels ((,bodfn ,parms ,@body))
		(loop for ,rest on ,src
		      while (nthcdr ,(1- len) ,rest)
		      do (,bodfn ,@(loop for ri to (1- len)
					 collect `(nth ,ri ,rest)))
		      finally (progn
				,@(loop for ri to (- len 2)
					collect `(,bodfn
						  ,@(loop for i from ri to (- len 2)
							  collect `(nth ,i ,rest))
						  ,@(loop for si from 0 to ri
							  collect `(nth ,si ,src))))
				nil)))))))))


;;;; This is tight!!!! Holy moly.
(defmacro do-tuples/c2 (parms source &body body)
  (if parms
      (with-gensyms (src rest bodfn)
	(let ((len (length parms)))
	  `(let ((,src ,source))
	    (when (nthcdr ,(1- len) ,src)
	      (labels ((,bodfn ,parms ,@body))
		(do ((,rest ,src (cdr ,rest)))
		    ((not (nthcdr ,(1- len) ,rest))
		     ,@(mapcar #'(lambda (args)
				   `(,bodfn ,@args))
			       (dt-args len rest src))
		     nil)
		  (,bodfn ,@(map1-n #'(lambda (n)
					`(nth ,(1- n)
					  ,rest))
				    len))))))))))

;; Doesn't look very efficient if tuples are very long.
;; But tuples generally are short. So...
(defun dt-args (len rest src)
  (map0-n #'(lambda (m)
	      (map1-n #'(lambda (n)
			  (let ((x (+ m n)))
			    (if (>= x len)
				`(nth ,(- x len) ,src)
				`(nth ,(1- x) ,rest))))
		      len))
	  (- len 2)))



(defmacro mvdo (binds (test &rest result) &body body)
  (let ((label (gensym))
	(temps (mapcar #'(lambda (b)
			   (if (listp (car b))
			       (mapcar #'(lambda (x)
					   (gensym))
				       (car b))
			       (gensym)))
		       binds)))
    `(let ,(mappend #'mklist temps)
      (mvpsetq ,@(mapcan #'(lambda (b var)
			     (list var (cadr b)))
			 binds
			 temps))
      (prog ,(mapcar #'(lambda (b var) (list b var))
		     (mappend #'mklist (mapcar #'car binds))
		     (mappend #'mklist temps))
	 ,label
	 (if ,test
	     (return (progn ,@result)))
	 ,@body
	 (mvpsetq ,@(mapcan #'(lambda (b)
				(if (third b)
				    (list (car b)
					  (third b))))
			    binds))
	 (go ,label)))))

;; (mvdo* (((px py) (pos player) (move player mx my))
;; 	((x1 y1) (pos obj1) (move obj1 (- px x1)
;; 				  (- py y1)))
;; 	((x2 y2) (pos obj2) (move obj2 (- px x2)
;; 				  (- py y2)))
;; 	((mx my) (mouse-vector) (mouse-vector))
;; 	(win nil (touch obj1 obj2))
;; 	(lose nil (and (touch obj1 player)
;; 		       (touch obj2 player))))
;;        ((or win lose) (if win 'win 'lose))
;;        (clear)
;;        (draw obj1)
;;        (draw obj2)
;;        (draw player))

(defmacro mvdo* (parm-cl test-cl &body body)
  (mvdo-gen parm-cl parm-cl test-cl body))

(defun mvdo-gen (binds rebinds test body)
  (if (null binds)
      (let ((label (gensym)))
	`(prog nil
	  ,label
	  (if ,(car test)
	      (return (progn ,@(cdr test))))
	  ,@body
	  ,@(mvdo-rebind-gen rebinds)
	  (go ,label)))
      (let ((rec (mvdo-gen (cdr binds) rebinds test body)))
	(let ((var/s (caar binds)) (expr (cadar binds)))
	  (if (atom var/s)
	      `(let ((,var/s ,expr)) ,rec)
	      `(multiple-value-bind ,var/s ,expr ,rec))))))

(defun mvdo-rebind-gen (rebinds)
  (cond ((null rebinds) nil)
	((< (length (car rebinds)) 3)
	 (mvdo-rebind-gen (cdr rebinds)))
	(t
	 (cons (list (if (atom (caar rebinds))
			 'setq
			 'multiple-value-setq)
		     (caar rebinds)
		     (third (car rebinds)))
	       (mvdo-rebind-gen (cdr rebinds))))))

(defmacro mvdo (binds (test &rest result) &body body)
  (let ((label (gensym))
	(temps (mapcar #'(lambda (b)
			   (if (listp (car b))
			       (mapcar #'(lambda (x)
					   (gensym))
				       (car b))
			       (gensym)))
		       binds)))
    `(let ,(mappend #'mklist temps)
      (mvpsetq ,@(mapcan #'(lambda (b var)
			     (list var (cadr b)))
			 binds
			 temps))
      (prog ,(mapcar #'(lambda (b var) (list b var))
		     (mappend #'mklist (mapcar #'car binds))
		     (mappend #'mklist temps))
	 ,label
	 (if ,test
	     (return (progn ,@result)))
	 ,@body
	 (mvpsetq ,@(mapcan #'(lambda (b)
				(if (third b)
				    (list (car b)
					  (third b))))
			    binds))
	 (go ,label)))))



(defun shuffle (x y)
  (cond ((null x) y)
	((null y) x)
	(t (list* (car x) (car y)
		  (shuffle (cdr x) (cdr y))))))

(defmacro mvpsetq (&rest args)
  (let* ((pairs (group args 2))
	 (syms (mapcar #'(lambda (p)
			   (mapcar #'(lambda (x) (gensym))
				   (mklist (car p))))
		       pairs)))
    (labels ((rec (ps ss)
	       (if (null ps)
		   `(setq
		     ,@(mapcan #'(lambda (p s)
				   (shuffle (mklist (car p))
					    s))
			       pairs syms))
		   (let ((body (rec (cdr ps) (cdr ss))))
		     (let ((var/s (caar ps))
			   (expr (cadar ps)))
		       (if (consp var/s)
			   `(multiple-value-bind ,(car ss)
			     ,expr
			     ,body)
			   `(let ((,@(car ss) ,expr))
			     ,body)))))))
      (rec pairs syms))))

;; (let ((w 0) (x 1) (y 2) (z 3))
;;   (mvpsetq (w x) (values 'a 'b) (y z) (values w x) r 3)
;;   (list w x y z))








;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; A general let
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; let*, multiple-value-bind, destructuring-bind