How to treat a function like a list?

Started by JeremyDunn, July 03, 2010, 11:52:43 PM

Previous topic - Next topic

JeremyDunn

I was trying to write what should be a simple function but found that I didn't know how to treat a function expression like a list. What I was trying to do was this; I wanted to write a function that I will call REV. What REV does is take a function expression as an argument and it executes the function except with its arguments in reverse order. So (rev (div 3 6)) executes

(div 6 3). This can be handy for rearranging an expression to a more aesthetic appearance. So how does one do something like this?

cormullion

#1
(define-macro (rev expr)
  (apply (expr 0) (reverse (rest expr))))

(rev (div 3 6))
;-> 2

JeremyDunn

#2
Thanks so much. I was thinking that I had to write a define statement rather than define-macro because I had a fixed number of  arguments rather than a variable number but these subtleties often elude me. To paraphrase Yogi Berra: LISP is so simple no one can understand it!