4 ms·
Why do you need the meta-capabilities of lisp? Well, language designers are human and can't predict all the features that you might find useful. Take, for examp
by vzcx 9y ago
Why do you need the meta-capabilities of lisp? Well, language designers are human and can't predict all the features that you might find useful. Take, for example, list comprehensions.
I added some basic ones to common lisp in a couple of minutes.
(defun read-comprehension (stream char)
(declare (ignore char))
(destructuring-bind (expr for var in list &optional if filter-exp)
(read-delimited-list #\] stream)
`(loop ,for ,var ,in ,list
,@(when filter-exp `(,if ,filter-exp))
collect ,expr)))
(set-macro-character #\[ #'read-comprehension)
(eval (read-from-string "[(+ x 1) for x in '(1 2 3) if (> x 2)]"))
;; => (4)
- jstimpfle 9y agoThis looks impressive on the first sight (kudos!). But on the other hand it's still a pretty fragile and unreadable macro, no? And it supports only a very limited form of list comprehensions. Can "destructuring-bind" support them fully?
- vzcx 9y agoYes, it is fragile (less than five minutes, man!) but hardly unreadable to the macro-accustomed eye. A "real" implementation would do more processing of the forms in the list read by read-delimited-list and perform appropriate checks. This is more to just give you a sense of how trivial it is to extend the language. I don't actually know python well enough (I had to look up "list comprehension") to say, but I would estimate that it would only take a page or so of code to give you something fully-featured and robust. In practice, I would never abuse the reader like this when typical "loop" expressions are enough to do these kind of things (and more).
- martinflack 9y agoAnyone who would like to use list comprehension forms, or read about how Lisp enables them, might find these papers interesting: Implementation of a "Lisp comprehension" macro by Guy Lapalme http://rali.iro.umontreal.ca/rali/sites/default/files/publis/LapalmeLispComp.pdf http://rali.iro.umontreal.ca/rali/sites/default/files/publis... Simple and Efficient Compilation of List Comprehension in Common Lisp by Mario Latendresse http://www.ai.sri.com/~latendre/listCompFinal.pdf http://www.ai.sri.com/~latendre/listCompFinal.pdf