Re: The LOOP macro

From: Edi Weitz (edi_at_agharta.de)
Date: 03/25/04


Date: Thu, 25 Mar 2004 02:06:02 +0100

On Wed, 24 Mar 2004 23:59:39 +0100, André Thieme <this.address.is.good.until.2004.apr.17@justmail.de> wrote:

> But to be fair, when a language X does not support a feature the
> users of X can always say that you can simply implement this
> feature. In the same time the users of language Y think: oh, we
> already have this and this looks like an advantage. So when we can
> say: "Ok, Lisp cannot do it but we can always program it in a way so
> it gets this feature", then we can also use the same argument for
> another language which does not directly support a feature which
> Lisp supports.

No, generally you can't. You are comparing apples to oranges.

Granted, we are all Lisp zealots and we think that all other languages
are junk but take a look at this nevertheless:

1. IIRC, some (all?) Schemes have an extension to COND where you can
   write '=>' after a test form such that if the test form evaluates
   to a true value the function behind the '=>' is applied to the
   result of the test form. You want that in Common Lisp? Wait...

     * (defmacro cond* (&rest conditions)
         (let ((=var= (gensym)))
           `(let (,=var=)
             (cond
               ,@(loop for condition in conditions
                       when (and (symbolp (second condition))
                                 (string= (symbol-name (second condition))
                                          "=>"))
                       collect (list `(setq ,=var= ,(first condition))
                                     `(funcall ,(third condition) ,=var=))
                       else
                       collect condition)))))

     COND*
     * (defun foo (x)
           (cond* ((eq 3 4) "Really?")
                  (x => (lambda (z) (format nil "Yes, ~A is it!" z)))
                  (t "Nothing found")))

     FOO
     * (foo nil)

     "Nothing found"
     * (foo 42)

     "Yes, 42 is it!"

2. Your code is full of dates and you'd really like to have a shortcut
   like !2001-11-09 instead of

     (ENCODE-UNIVERSAL-TIME 0 0 0 9 11 2001)

   Well, ok...

     * (defun date-reader (stream char)
         (declare (ignore char))
         (let ((exp (read stream t nil t)))
           (when (symbolp exp)
             (let ((y-m-d (nth-value 1
                            (cl-ppcre:scan-to-strings "^(\\d{4})-(\\d{2})-(\\d{2})"
                                                      (symbol-name exp)))))
               (return-from date-reader
                 (apply #'encode-universal-time 0 0 0
                        (nreverse (map 'list #'parse-integer y-m-d))))))
           (error "Ooops...")))

     DATE-READER
     * (set-macro-character #\! #'date-reader)

     T
     * !2004-03-25

     3289158000
     * (decode-universal-time *)

     0
     0
     0
     25
     3
     2004
     3
     NIL
     -1

   (Of course, these values are inserted into your code at read time,
   not at run time.)

So, OK, these are silly and probably buggy examples but I hope you get
the idea: Can you add new control structures to Java and Python or
change their read syntax without mucking with the
compiler/interpreter? Compared to most other languages Lisp is almost
infinitely extendable.

Edi.



Relevant Pages

  • Re: The LOOP macro
    ... when a language X does not support a feature the users ... "Ok, Lisp cannot do it but we can always program it ... In Lisp I might start with a closure for a problem while in Java I would ...
    (comp.lang.lisp)
  • Re: Hows dot.net doing nowadays?
    ... Actually there are feature more important than others if you look at ... The ability to combined subroutines with the data ... The feature that gives the most trouble is Lisp ability to ... the database engine but Lisp can do it within the language itself. ...
    (microsoft.public.vb.general.discussion)
  • Re: Which programming language is better to start
    ... >>> Macros are the feature that allows syntactic abstraction (and makes it ... making it possible to extend the language with new operators ... LISP macros are unsafe whereas camlp4 is not. ... I've heard that it is much easier to introduce bugs with Lisp macros as ...
    (comp.programming)
  • Re: is haskell more productive than scheme?
    ... Lisp in general claims o be able to copy any feature of any language. ... Lisp cannot easily copy guarantees that other languages may offer. ... E.g. Haskell guarantees that all Haskell functions do not have side effects. ...
    (comp.lang.functional)
  • Re: ILC2005: McCarthy denounces Common Lisp, "Lisp", XML, and Rahul
    ... >> the language should be available to users. ... In the design of Common Lisp, I asked Dave Moon (one of the architects ... Now, there are good kinds of low-level, like the way that floats are ...
    (comp.lang.lisp)