#| Copyright (c) 2005 Christopher K. Riesbeck Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |# ;;; BIND from Chapter 3, Artificial Intelligence Programming ;;; Redefined using parser and expander functions. A little longer, but more ;;; maintainable. (defmacro bind (locs-n-vals &body body) (let ((setf-methods (get-setf-methods locs-n-vals)) (vals (get-values locs-n-vals)) (symbols (get-symbols locs-n-vals))) (expand-bind (collect-lists #'first setf-methods) (collect-lists #'second setf-methods) (collect-cars #'third setf-methods) (collect-items #'fourth setf-methods) (collect-items #'fifth setf-methods) vals symbols body))) (defun get-setf-methods (locs-n-vals) (mapcar #'(lambda (loc-n-val) (multiple-value-list (get-setf-method (first loc-n-val)))) locs-n-vals)) (defun get-symbols (locs-n-vals) (mapcan #'(lambda (loc-n-val) (if (symbolp (first loc-n-val)) (list (first loc-n-val)) nil)) locs-n-vals)) (defun collect-items (accessor l) (mapcar accessor l)) (defun collect-lists (accessor l) (mapcan #'(lambda (x) (copy-list (funcall accessor x))) l)) (defun collect-cars (accessor l) (mapcar #'(lambda (x) (car (funcall accessor x))) l)) (defun expand-bind (temp-vars temp-vals store-vars store-forms access-forms vals symbols body) `(let* (,@(pair-up temp-vars temp-vals) ,@(pair-up store-vars access-forms)) ,@(make-declares symbols) (unwind-protect (let* ,(pair-up store-vars vals) ,@store-forms ,@body) ,@store-forms))) (defun pair-up (l1 l2) (mapcar #'list l1 l2)) (defun make-declares (symbols) (if (null symbols) nil `((declare (special ,@symbols)))))