Page 1 of 1

How to treat a function like a list?

Posted: Sun Jul 04, 2010 6:52 am
by 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?

Re: How to treat a function like a list?

Posted: Sun Jul 04, 2010 7:29 am
by cormullion

Code: Select all

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

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

Re: How to treat a function like a list?

Posted: Sun Jul 04, 2010 4:54 pm
by JeremyDunn
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!