newLISP Fan Club

Forum => newLISP newS => Topic started by: cormullion on November 08, 2006, 09:38:34 AM

Title: calling context functions indirectly
Post by: cormullion on November 08, 2006, 09:38:34 AM
What I'm trying to do here is pass functions and contexts around, but I can't get the functions to execute.


(context 'TEST0)
  (define (TEST0:foo)  
    (println "hi there from TEST0:foo"))
  (define (TEST0:bar)  
    (println "hi there from TEST0:bar"))

(context 'TEST1)
  (define (TEST1:foo)  
    (println "hi there from TEST1:foo"))
  (define (TEST1:bar)  
    (println "hi there from TEST1:bar"))

(context 'TEST2)
  (define (TEST2:foo)  
    (println "hi there from TEST2:foo"))
  (define (TEST2:bar)  
    (println "hi there from TEST2:bar"))
 
(context MAIN)
  (set 'contexts '(TEST0 TEST1 TEST2))
  (set 'functions '({foo} {bar}))
  (dolist (c contexts)
    (map (fn (f) (eval (sym f c))) functions)             ; <- doesn't do what I thought it might :-)
    )


The required output is for all available contexts to run their functions when called upon to do so. eval should be able to execute the function...?
Title:
Post by: rickyboy on November 08, 2006, 10:09:51 AM
Aren't you missing an extra pair of parentheses, i.e. around the eval call?



--Rick
Title:
Post by: cormullion on November 08, 2006, 10:54:22 AM
Quote from: "rickyboy"Aren't you missing an extra pair of parentheses, i.e. around the eval call?



--Rick


yes, I am! Thanks for spotting that - it wouldn't have looked right to me so i would never have tried it!



cheers
Title:
Post by: Lutz on November 08, 2006, 11:50:21 AM
Rick's recommendation is correct, but there is an (perhaps easier to understand?) alternative using 'apply':


(context MAIN)
  (set 'contexts '(TEST0 TEST1 TEST2))
  (set 'functions '({foo} {bar}))
  (dolist (c contexts)
    (map (fn (f) (apply (sym f c))) functions)  
    )


The effect is the same. Rick's extra parenthesis puts the lambda expression contained in the symbol (sym f c) and extracted by 'eval' into an applicative contextus. 'apply' does the same.



Lutz
Title:
Post by: cormullion on November 08, 2006, 02:00:36 PM
Thanks. "applicative contextus", eh? My Latin is not as fluent as my newlisp these days...



It's been an educational week, what with all that integer business as well...