Page 2 of 2

Posted: Mon Apr 06, 2009 9:16 pm
by newdep
I cant get closer then this to scheme..

Code: Select all

(define-macro (my-or)
 (let (temp 
  (unless (eval (args 0))  
   (eval (args 1))))   
    temp))  

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

At least its short (shorter then the if version)
returns the values correctly and doesnt drop variables..

But then again it uses (args) and it uses 'unless (which is an 'or).


PS: Dropin another quizzzzzzzzzz ;-)

Posted: Mon Apr 06, 2009 10:20 pm
by xytroxon
newdep wrote:(define-macro (my-or)
(not (and (eval (args 0)) (eval (args 1))))
)

but probably thats a sidekick ;-)
LOL!!!

I love these code challenges :-)

But the quest should always be for the shortest path...

In other LISPs "eval" can be a speed hog (major slow down)...

-- xytroxon

Posted: Mon Apr 06, 2009 10:44 pm
by itistoday
newdep wrote:I cant get closer then this to scheme..

Code: Select all

(define-macro (my-or)
 (let (temp 
  (unless (eval (args 0))  
   (eval (args 1))))   
    temp))  

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

At least its short (shorter then the if version)
returns the values correctly and doesnt drop variables..

But then again it uses (args) and it uses 'unless (which is an 'or).


PS: Dropin another quizzzzzzzzzz ;-)
Congrats newdep! That's a valid solution!

It is similar in principle to the one I came up with, but actually yours is better because it is much faster. In fact it's almost as fast as placing the macro in its own namespace. Send me a PM with your full name and email if you'd like a license to Espionage.

Here is the one I came up with, as you can see the principle of avoiding the evaluation is sortof the same, whereas you do it with 'unless', I did it with the exception mechanism. :-D

Code: Select all

(define-macro (my-or)
	(catch
		(begin
			(let (temp (eval (args 0)))
				(if temp (throw temp))
			)
			(throw (eval (args 1)))
		)
	)
)

Posted: Mon Apr 06, 2009 11:00 pm
by itistoday
Yes, the 'unless' and 'or' are similar, but the 'or' solution as was presented was equivalent to doing this (and therefore not an acceptable solution):

Code: Select all

(set 'my-or or)
I was mainly looking for something similar to what I had, which I feel your solution was, as it used the same principle of avoiding the extra evaluation that was in DrDave's code while avoiding variable capture as well.

I hope though that nobody actually writes their macros this way, as to an outsider it would be completely non-obvious why the extra hoop-jumping was taking place.

This just demonstrates the need for a define-smacro in newLISP.

Posted: Tue Apr 07, 2009 7:14 am
by newdep
Aha...I win finaly something here ;-)
Thanks for the price.. I dont have a Mac with OSX only
a MacClassic and an IMac.. But If you dont mind I would Like to store
you kind generosity for the "yearly newlisp Contest".. Where a winner
then also could get your nice software package.. (If thats oke with you, it would be a nice addon for the price shelf...)

Coming back to the Scheme example, I think its somehow unfair too what
Scheme does with GenSym. So personaly I prefer the newlisp (arg) way
of solving this. Where GenSym is a nice workaround its also not transparent for the function..

..I liked the quizzzz.. nice brain trainers these are...

Posted: Tue Apr 07, 2009 5:11 pm
by itistoday
newdep wrote:But If you dont mind I would Like to store
you kind generosity for the "yearly newlisp Contest".. Where a winner
then also could get your nice software package.. (If thats oke with you, it would be a nice addon for the price shelf...)
Sure! :-)

Avoid var capture by avoiding variables?

Posted: Wed Apr 08, 2009 8:50 pm
by cgs1019

Code: Select all

(define-macro (my-or)
  (
    (lambda ()
      (if (args 0)
        (args 0)
         (eval (args 1))
      )
    )
    (eval (args 0))
    (args 1)
  )
)
This solution uses a lambda to store the eval'ed first arg to the macro and defer evaluation of the second. It's not as fast as the solution presented by lutz, which has an execution time about 65% of mine, probably because mine entails an extra function call. But it does, at least, avoid the need for creating a new context just for one simple macro. This was a fun and surprisingly challenging problem. Thanks, itistoday!

Re: Avoid var capture by avoiding variables?

Posted: Wed Apr 08, 2009 9:04 pm
by Kazimir Majorinc
So you used nested function call to transfer arguments in nested function so you do not have to use any variable at all. As (arg n) are always local, accidental overshadowing is impossible. Very interesting idea.

Welcome to the forum.

Re: Avoid var capture by avoiding variables?

Posted: Wed Apr 08, 2009 9:19 pm
by xytroxon
my-or needs a better name...

Safe OR Eval

sore

or maybe

saf-or-e

Pronounced "safari".

In honor of the safe journey through the newLISP namespace jungle ;)

-- xytroxon

Re: Avoid var capture by avoiding variables?

Posted: Wed Apr 08, 2009 9:24 pm
by cgs1019
Kazimir Majorinc wrote:So you used nested function call to transfer arguments in nested function so you do not have to use any variable at all. As (arg n) are always local, accidental overshadowing is impossible. Very interesting idea.

Welcome to the forum.
Thanks! While this particular problem is unlikely to arise in any real-life work (after all, we have 'or), I think the basic underlying idea is potentially quite useful.

Posted: Thu Apr 09, 2009 7:56 am
by Lutz
Nice full functional solution cgs1019!

Here a rewrite easier to understand for beginners:

Code: Select all

(define-macro (my-or) 
  (let ( func (lambda () (if (args 0) (args 0) (eval (args 1)) )))
    (func (eval (args 0)) (args 1))
  )
) 
cgs1019 passes the user arguments to an inner function, with only the first argument evaluated. The inner function then decides if the second must be evaluated too. cgs1019 just uses the anonymous version of func. This is like doing:

Code: Select all

( (lambda (x) (+ x x)) 1) => 2
ps: apropos "namespace jungle ;)": basic usage patterns of namespaces in newLISP are quite simple, like partitioning code into modules. Beyond that it gets more complicated and requires some manual studying to understand that namespaces in newLISP are not dynamic closures like in Scheme, but can be just as (and more) powerful when using them in their own way ;-)

Re: Avoid var capture by avoiding variables?

Posted: Thu Apr 09, 2009 3:47 pm
by itistoday
cgs1019 wrote:This solution uses a lambda to store the eval'ed first arg to the macro and defer evaluation of the second. It's not as fast as the solution presented by lutz, which has an execution time about 65% of mine, probably because mine entails an extra function call. But it does, at least, avoid the need for creating a new context just for one simple macro. This was a fun and surprisingly challenging problem. Thanks, itistoday!
Your solution is beautiful Chris, I'd give you an Espionage license for it if I didn't know that you already had one. :-)

Posted: Thu Apr 09, 2009 7:55 pm
by newdep
( (lambda (x) (+ x x)) 1) => 2

...efficient thinking is not always efficient for the thinking...
I forget about these powerfull solutions too often..
Nice nice..!