How to treat a function like a list?

Q&A's, tips, howto's
Locked
JeremyDunn
Posts: 11
Joined: Wed Apr 07, 2010 12:18 am

How to treat a function like a list?

Post 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?

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: How to treat a function like a list?

Post by cormullion »

Code: Select all

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

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

JeremyDunn
Posts: 11
Joined: Wed Apr 07, 2010 12:18 am

Re: How to treat a function like a list?

Post 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!

Locked