Is it possible to write c...r in lisp, for any combination of letters a and d?
Hello, lispers!
Is it posible to write macro (or something) in lisp, such that any 'call' which has shape like (caddaddr x) automatically expands into (car (cdr (cdr (car (cdr (cdr x)))))) Naturally, this should work for any possible combination of letters a and d. Is that possible?
18 comments
[ 170 ms ] story [ 4219 ms ] thread[Edit:
I see that he said any call .. well, he will have to define the function before hand before he calls it. That's just common sense.
Apologies dfox.]
It's pretty straightforward to do what you want by writing a reader macro, but then you have to prefix your sexp with some special macro character.
If the prefix is a deal breaker there may be some deep read-macro voodoo that could do what you want but, but before doing research, I have to ask the obvious question: "Why?".
Incidentally, pg's On Lisp has a decent intro to reader macros (http://www.bookshelf.jp/texi/onlisp/onlisp_18.html)
Come'on, it's a nice trivia question. Let's light this joint up with parentheses folks! Schemers, Clojeristas ..
Which makes it sound like there is in fact no way to do this in standard CL, to say nothing of other lisps.
Untested perl code to do this (taking a cons to be a [car, cdr] array reference):
Example of a situation this feature might be useful: "I have this object with lots of properties which I don't necessarily know what they are. How do I define getter/setter methods on them?"Threads are not "standard" but the last thread-less Common Lisp just got them this month. Every maintained Common Lisp implementation today has Threads; and all support Bordeaux-Threads.
Show me a single mainstream language where threads are "standardized" other than Java?
My awkward phrasing was a reference to about 1:53 in this: http://www.youtube.com/watch?v=bCWA7uevo_Q
Before you go far and wide on speculation alone, you might stop and reread what has been posted so far, again. Both me and lnostdal submitted working CL solutions.
Example of a situation this feature might be useful: "I have this object with lots of properties which I don't necessarily know what they are. How do I define getter/setter methods on them?"
;; test cases
;; the base works, let's test the syntax; NB, redefining a builtin will not work :-)just off the top of my head in 8 minutes.
make it to a reader macro if you so wish.
[Edit:
cat >> nfs://cltl3.alu.org/todo.txt
* CASE with custom tests
* COMP^H^H^H^HArnesi FFS!
]
Also, that eval could be replaced with (untested)
(defmacro blah (arg body) (with-input-from-string (stream (reverse (string arg))) (dolist (char (loop :for char = (read-char stream nil) :while (and char (or (char= char #\A) (char= char #\D) (error "BLAH: Only A or D is accepted."))) :collect char)) (case char (#\A (setf body `(car ,body))) (#\D (setf body `(cdr ,body))))) body))
CL-USER> (let ((data '((42)))) (blah aa data)) 42 CL-USER>
..manipulate the reader using read-macros (see TCR's new named-readtables library) if you want anything fancier. check out this btw.; http://groups.google.com/group/comp.lang.lisp/msg/d2425c92ce...
edit: the web still doesn't work as i want it to work .. i'm not going to bother fixing the whitespace etc. here