Something that spawned from this discussion was an interesting function called 'my-or' that I found in this Scheme tutorial.
my-or is a macro, but not just any macro, it's a safe one. It takes exactly two arguments and evaluates the first argument only once, and it's immune to variable capture. If the result of that evaluation is true, then that value is returned, otherwise, the result of evaluating the second argument is returned.
Sounds simple? Try it, it's not. Here, I'll help you out (evil grin). Here is the solution as written in Scheme:
Code: Select all
(define-macro my-or
(lambda (x y)
(let ((temp (gensym)))
`(let ((,temp ,x))
(if ,temp ,temp ,y)))))
Code: Select all
(set 'temp 45)
(println "(my-or temp nil) = " (my-or temp nil))
(println "(my-or nil temp) = " (my-or nil temp))
(println "-----------")
(set 'value (my-or (begin (println "first arg") 1) (begin (println "second arg") 2)))
(println "should be 1: " value)
(println "-----------")
(set 'value (my-or (begin (println "first arg") nil) (begin (println "second arg") 2)))
(println "should be 2: " value)
The winner is the first person to get the following output exactly:
Code: Select all
(my-or temp nil) = 45
(my-or nil temp) = 45
-----------
first arg
should be 1: 1
-----------
first arg
second arg
should be 2: 2
Hmm... since I'm also the developer of Espionage (OS X Leopard only), that is something that I can offer to the winner (if they want it). Of course you'll also receive the warm fuzzy feeling that comes with solving such challenges. :-)
Post your solution below if you've got it, I will give the solution on April 11th if no one solves it in that time.
Note: based on the solution presented below by Kazimir, the rules have been modified slightly, read my reply to him for the update.
Update: One solution has been presented by contestant Kazimir! But there's still another, faster solution possible!
Challenge is over! Congrats to Kazimir and newdep!