13 points · by DaveKruglinsky · 17y ago · 18 comments
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?
[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.]
(defun charfn (char)
"maps a character to a function"
(case char
((or #\A #\a) #'car)
((or #\D #\d) #'cdr)
(t (error "unknown character-function type ~a" char))))
(defun fn-list (decon-name)
(mapcar #'charfn (butlast (rest (coerce (symbol-name decon-name) 'list)))))
;;; syntax (defmacro defdecon (fn-name)
`(defun ,fn-name (args)
(funcall (arnesi:compose ,@(fn-list fn-name)) args)))
;;; play with it;; test cases
(funcall (arnesi:compose 'car) (list 1 2 3)) => 1
(funcall (arnesi:compose 'caar) (list (list 1) 2 3)) => 1
;; the base works, let's test the syntax; NB, redefining a builtin will not work (defdecon caaaaar) ; define first
(caaaaar (list (list (list (list (list 1 #\c) #\b) #\a) 2) 3)) ==> 1
:-)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!
]
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)
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):
AUTOLOAD {
unless ($AUTOLOAD =~ m/::c([ad]{1,})r$/) {
die "Attempted to call undefined function $AUTOLOAD";
}
my $ads = $1;
my $cons = shift;
while (my $c = chop $ads) {
$cons = $cons->[$c eq 'a' ? 0 : 1];
}
return $cons;
}
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?"Come'on, it's a nice trivia question. Let's light this joint up with parentheses folks! Schemers, Clojeristas ..
LISP_METH = /^c[ad]+r$/
def method_missing(name, *args)
meth = name.to_s
raise NoMethodError, "undefined method '#{name}'" unless meth =~ LISP_METH
list = args.first
calls = meth.split('')[1..-2].reverse
calls.each {|call| list = (call == 'a' ? list.first : list[1..-1]) }
list
end
cadr [1,2,3]
# => 2
caaddddr [1,2,3,4,[9,8,7]]
# => 9
cadadr [1,[9,[100],8,7],3,4]
# => 100 (defn make-xrs [n]
(if (= n 0) {"ar" first, "dr" rest}
(let [last (make-xrs (dec n))]
(into last (for [[name fun] last [name2 fun2] {"d" rest "a" first}]
[(str name2 name) (comp fun2 fun)])))))
(for [[name fun] (make-xrs 10)]
(eval `(def ~(symbol (str "c" name)) ~fun)))Also, that eval could be replaced with (untested)
(intern *ns* (symbol (str "c" name)) fun)(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