newLISP Challenge: The seemingly simple 'my-or'

Started by itistoday, April 04, 2009, 04:22:19 PM

Previous topic - Next topic

itistoday

#30
Quote from: "newdep"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.

cgs1019

#31

(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!
-- Christopher Suter



\"The only reason for time is so that everything doesn\'t happen at once.\"

Albert Einstein

Kazimir Majorinc

#32
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.
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

xytroxon

#33
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

cgs1019

#34
Quote from: "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.


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

Lutz

#35
Nice full functional solution cgs1019!



Here a rewrite easier to understand for beginners:


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


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

itistoday

#36
Quote from: "cgs1019"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.

newdep

#37
( (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))