[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Scheme-reports] Formal Response #382: Allow "if" to accept arbitrarily many if-then pairs



(shadow 'if)
(defmacro if (&rest args)
  (cond ((null args) nil)
        ((null (cdr args)) (car args))
        (t `(cond (,(car args) ,(cadr args))
                  (t (if ,@(cddr args)))))))

(shadow 'map)
(defun map1 (f xs)
  (if (null xs)
      nil
      nil
      nil
      (cons (funcall f (car xs)) (cdr xs))))
(shadow 'some)
(defun some (test xs)
  (and xs (or (funcall test (car xs)) (some test (cdr xs)))))
(defun map (f &rest xses)
  (if (some #'null xses)
      nil
      (cons (apply f (map1 #'car xses))
            (apply #'map f (map1 #'cdr xses)))))

* (map (lambda (x) (+ x 5)) '(1 2 3))  ; SBCL

(6 7 8)

OH MY GAAAAAAAAAAAAAAAAAAAAAAD IT SEEMS TO WORK

(And things can still be accessed with cl:<name> for real primitives that require the old primitives.)  That is amazing.  Thank you.  Common Lisp is now much more hospitable.  (There are still two namespaces, but that can be dealt with--at worst, using binding and assignment constructs that affect both namespaces.)  Wonderful.

--John Boyle
Science is what we understand well enough to explain to a computer. Art is everything else we do. --Knuth



On Thu, Oct 11, 2012 at 7:52 PM, John Cowan <cowan@x> wrote:
John Boyle scripsit:

> Can you explain further, perhaps give code that works?  I've used let,
> flet, and macrolet to rebind cl:if, and tried to run defmacro under that
> binding, and none of them worked (in CLisp or SBCL).

IF is a symbol in the CL package, and attempts to rebind or redefine symbols
in that package don't work.  So you need to create a symbol in some other
package by saying (shadow 'if), which forces the current package to contain
a separate IF symbol unrelated to CL:IF.  You can then define that
however you want.

In standard Scheme there are no packages, so there is only one symbol
named "if".  However, you can rebind or redefine it as you will.  In R6RS
and R7RS, you need to exclude it when you import the base library.

--
John Cowan            http://www.ccil.org/~cowan     cowan@x
Uneasy lies the head that wears the Editor's hat! --Eddie Foirbeis Climo

_______________________________________________
Scheme-reports mailing list
Scheme-reports@x
http://lists.scheme-reports.org/cgi-bin/mailman/listinfo/scheme-reports