View unanswered posts | View active topics It is currently Thu Sep 09, 2010 4:24 am



Post new topic Reply to topic  [ 38 posts ]  Go to page Previous  1, 2
newLISP Challenge: The seemingly simple 'my-or' 
Author Message

Joined: Mon Feb 23, 2004 7:40 pm
Posts: 1943
Location: Netherlands
Post 
I cant get closer then this to scheme..
Code:
(define-macro (my-or)
(let (temp
  (unless (eval (args 0)) 
   (eval (args 1))))   
    temp)) 


Code:
(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))


Mon Apr 06, 2009 9:16 pm
Profile

Joined: Tue Nov 06, 2007 3:59 pm
Posts: 188
Post 
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


Mon Apr 06, 2009 10:20 pm
Profile WWW

Joined: Sun Dec 02, 2007 5:10 pm
Posts: 386
Location: Florida
Post 
newdep wrote:
I cant get closer then this to scheme..
Code:
(define-macro (my-or)
(let (temp
  (unless (eval (args 0)) 
   (eval (args 1))))   
    temp)) 


Code:
(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:
(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.


Mon Apr 06, 2009 10:44 pm
Profile WWW

Joined: Sun Dec 02, 2007 5:10 pm
Posts: 386
Location: Florida
Post 
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:
(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.


Mon Apr 06, 2009 11:00 pm
Profile WWW

Joined: Mon Feb 23, 2004 7:40 pm
Posts: 1943
Location: Netherlands
Post 
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))


Tue Apr 07, 2009 7:14 am
Profile

Joined: Sun Dec 02, 2007 5:10 pm
Posts: 386
Location: Florida
Post 
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.


Tue Apr 07, 2009 5:11 pm
Profile WWW

Joined: Wed Apr 08, 2009 8:12 pm
Posts: 5
Location: Gainesville, FL
Post Avoid var capture by avoiding variables?
Code:
(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!


Wed Apr 08, 2009 8:50 pm
Profile WWW

Joined: Thu May 08, 2008 1:24 am
Posts: 321
Location: Croatia
Post Re: Avoid var capture by avoiding variables?
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.

_________________
Institute for Programming; Kazimir Majorinc's blog


Wed Apr 08, 2009 9:04 pm
Profile WWW

Joined: Tue Nov 06, 2007 3:59 pm
Posts: 188
Post Re: Avoid var capture by avoiding variables?
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


Wed Apr 08, 2009 9:19 pm
Profile WWW

Joined: Wed Apr 08, 2009 8:12 pm
Posts: 5
Location: Gainesville, FL
Post Re: Avoid var capture by avoiding variables?
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


Wed Apr 08, 2009 9:24 pm
Profile WWW

Joined: Thu Sep 26, 2002 4:45 pm
Posts: 4159
Location: Boca Raton, Florida
Post 
Nice full functional solution cgs1019!

Here a rewrite easier to understand for beginners:

Code:
(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:
( (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 ;-)


Thu Apr 09, 2009 7:56 am
Profile E-mail WWW

Joined: Sun Dec 02, 2007 5:10 pm
Posts: 386
Location: Florida
Post Re: Avoid var capture by avoiding variables?
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. :-)

_________________
Get your Objective newLISP groove on.


Last edited by itistoday on Thu Apr 09, 2009 8:06 pm, edited 1 time in total.



Thu Apr 09, 2009 3:47 pm
Profile WWW

Joined: Mon Feb 23, 2004 7:40 pm
Posts: 1943
Location: Netherlands
Post 
( (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))


Thu Apr 09, 2009 7:55 pm
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 38 posts ]  Go to page Previous  1, 2


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.