newLISP Challenge: The seemingly simple 'my-or'

Pondering the philosophy behind the language

Postby newdep » Mon Apr 06, 2009 9:16 pm

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 ;-)
-- (define? (Cornflakes))
newdep
 
Posts: 2019
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Postby xytroxon » Mon Apr 06, 2009 10:20 pm

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
"Many computers can print only capital letters, so we shall not use lowercase letters."
-- Let's Talk Lisp (c) 1976
xytroxon
 
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm

Postby itistoday » Mon Apr 06, 2009 10:44 pm

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)))
      )
   )
)
Get your Objective newLISP groove on.
itistoday
 
Posts: 413
Joined: Sun Dec 02, 2007 5:10 pm
Location: I'm sitting right next to you. :)

Postby itistoday » Mon Apr 06, 2009 11:00 pm

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.
Get your Objective newLISP groove on.
itistoday
 
Posts: 413
Joined: Sun Dec 02, 2007 5:10 pm
Location: I'm sitting right next to you. :)

Postby newdep » Tue Apr 07, 2009 7:14 am

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...
-- (define? (Cornflakes))
newdep
 
Posts: 2019
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Postby itistoday » Tue Apr 07, 2009 5:11 pm

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! :-)
Get your Objective newLISP groove on.
itistoday
 
Posts: 413
Joined: Sun Dec 02, 2007 5:10 pm
Location: I'm sitting right next to you. :)

Avoid var capture by avoiding variables?

Postby cgs1019 » Wed Apr 08, 2009 8:50 pm

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!
cgs1019
 
Posts: 5
Joined: Wed Apr 08, 2009 8:12 pm
Location: Gainesville, FL

Re: Avoid var capture by avoiding variables?

Postby Kazimir Majorinc » Wed Apr 08, 2009 9:04 pm

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.
Kazimir Majorinc
 
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia

Re: Avoid var capture by avoiding variables?

Postby xytroxon » Wed Apr 08, 2009 9:19 pm

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
"Many computers can print only capital letters, so we shall not use lowercase letters."
-- Let's Talk Lisp (c) 1976
xytroxon
 
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm

Re: Avoid var capture by avoiding variables?

Postby cgs1019 » Wed Apr 08, 2009 9:24 pm

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.
-- Christopher Suter

"The only reason for time is so that everything doesn't happen at once."
Albert Einstein
cgs1019
 
Posts: 5
Joined: Wed Apr 08, 2009 8:12 pm
Location: Gainesville, FL

Postby Lutz » Thu Apr 09, 2009 7:56 am

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 ;-)
Lutz
 
Posts: 5279
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Re: Avoid var capture by avoiding variables?

Postby itistoday » Thu Apr 09, 2009 3:47 pm

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. :-)
Last edited by itistoday on Thu Apr 09, 2009 8:06 pm, edited 1 time in total.
Get your Objective newLISP groove on.
itistoday
 
Posts: 413
Joined: Sun Dec 02, 2007 5:10 pm
Location: I'm sitting right next to you. :)

Postby newdep » Thu Apr 09, 2009 7:55 pm

( (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..!
-- (define? (Cornflakes))
newdep
 
Posts: 2019
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Previous

Return to Whither newLISP?

Who is online

Users browsing this forum: No registered users and 1 guest

cron