redirecting functions

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

redirecting functions

Post by cormullion »

Ahoy there!

What's the best way to do this:

Code: Select all

(define (task)
   (action))

(if (= x 3)
  (set 'action '(sin x))
  (set 'action '(pow x 5)))

(set 'x 4)

(task)
Ie you want the 'action' function to do different things when it's called.

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

Code: Select all

(if (= x 3) 
  (set 'action '(sin x)) 
  (set 'action '(pow x 5)))

(eval (expand action 'x))
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

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

Post by cormullion »

Thanks Jeff. Eventually I decided to do it using "fn".

Code: Select all

 (set 'do-content (fn (f) (single-entry-by-id (CGI:get {entry-id}))
this doesn't evaluate till I want to call it. There's probably a better way, but this works for now... :-)

jrh
Posts: 36
Joined: Mon Nov 14, 2005 9:54 pm
Location: Portland, Oregon

Post by jrh »

God, what a mongrelized bastard newLISP is! (this is a good thing.)

With C library support and sockets, a TCL/TK interface, and now Java graphics it's the language of The Day of the Triffids. Watch out, you will be assimilated!

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

Post by cormullion »

jrh wrote:God, what a mongrelized bastard newLISP is! (this is a good thing.)

With C library support and sockets, a TCL/TK interface, and now Java graphics it's the language of The Day of the Triffids. Watch out, you will be assimilated!
That's one way of putting it! :-) Or you could say that newLISP is a good citiizen and a considerate neighbor. Not so dramatic, though..!

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

You could do it without the eval if you want it to not evaluate yet:

Code: Select all

(if (= x 3) 
  (set 'action '(sin x)) 
  (set 'action '(pow x 5))) 

(setq to-be-evaled-later (expand action 'x))
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

Locked