newLISP Challenge: The seemingly simple 'my-or'

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

Previous topic - Next topic

DrDave

#15
Does this work for you?


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

    )
)




results:


(my-or temp nil) = 45
(my-or nil temp) = 45
-----------
first arg
should be 1: 1
-----------
first arg
second arg
should be 2: 2
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.

\"Getting Started with Erlang\"  version 5.6.2

m i c h a e l

#16
Here's a minimal version:


(define-macro (my-or)
   (or (eval (args 0)) (eval (args 1)))
)


m i c h a e l

itistoday

#17
Quote from: "DrDave"Does this work for you?



results:


How did you get those results? With that code the second argument is always evaluated, therefore producing this result:


(my-or temp nil) = 45
(my-or nil temp) = 45
-----------
first arg
second arg
should be 1: 1
-----------
first arg
second arg
should be 2: 2
Get your Objective newLISP groove on.

itistoday

#18
Quote from: "m i c h a e l"Here's a minimal version:


(define-macro (my-or)
   (or (eval (args 0)) (eval (args 1)))
)


That's cheating. :-p



I guess I didn't state it, but part of the idea/challenge is that you're supposed to define your own 'or', if you could use the built-in 'or' then it wouldn't really be a challenge..
Get your Objective newLISP groove on.

DrDave

#19
Quote from: "itistoday"
How did you get those results? With that code the second argument is always evaluated, therefore producing this result:


(my-or temp nil) = 45
(my-or nil temp) = 45
-----------
first arg
second arg
should be 1: 1
-----------
first arg
second arg
should be 2: 2


Ah, my bad. You're right. I didn't look carefully at the results.
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.

\"Getting Started with Erlang\"  version 5.6.2

xytroxon

#20
Quote from: "itistoday"
Quote from: "m i c h a e l"Here's a minimal version:


(define-macro (my-or)
   (or (eval (args 0)) (eval (args 1)))
)


That's cheating. :-p



I guess I didn't state it, but part of the idea/challenge is that you're supposed to define your own 'or', if you could use the built-in 'or' then it wouldn't really be a challenge..


Inheritance is a powerful (and in 2009), a fundamental programming concept... For OOP, or for LISP, which at it's core "invented" inheritance by it's "functional" programming language nature... Every new function "inherits" code from existing functions... "or" is the base function... "my-or" is the derived functionality that reuses the "or" code and extends it's functionality... Using the "if" function is no more, or no less a valid idiom than using the "or" function...



Michael's version is clear, concise, and I can understand at a glance what it is doing... It is "pure"... It is "beautiful"... The only potential fault with such "beauty" is that it must also work... In real world programming, functionality must always trump beautiful form... When both functional form and beauty come together, it is rare, and profound...



"OR" is this to be an obfuscated newLISP contest? ;-)



-- xytroxon
\"Many computers can print only capital letters, so we shall not use lowercase letters.\"

-- Let\'s Talk Lisp (c) 1976

itistoday

#21
Not an obfuscated contest, no, just a challenge to see if you can figure out a way to do it. While working on this problem myself I came up with what I thought was a clever little solution and I thought it would make for a fun topic/challenge. :-)
Get your Objective newLISP groove on.

newdep

#22
(define-macro (my-or)

   (not (and (eval (args 0)) (eval (args 1))))

)



but probably thats a sidekick ;-)
-- (define? (Cornflakes))

itistoday

#23
Quote from: "newdep"but probably thats a sidekick ;-)


Yeah... but in addition that doesn't produce the correct results:


(my-or temp nil) = true
(my-or nil temp) = true
-----------
first arg
second arg
should be 1: nil
-----------
first arg
should be 2: true
Get your Objective newLISP groove on.

newdep

#24
yeah Im still looking into it because I want small code result ;-)

So i like micheal solution but im aming for a more scheme lookalike..
-- (define? (Cornflakes))

newdep

#25
I cant get closer then this to scheme..

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


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

xytroxon

#26
Quote from: "newdep"(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

itistoday

#27
Quote from: "newdep"I cant get closer then this to scheme..

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


(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. ](define-macro (my-or)
   (catch
      (begin
         (let (temp (eval (args 0)))
            (if temp (throw temp))
         )
         (throw (eval (args 1)))
      )
   )
)[/code]
Get your Objective newLISP groove on.

itistoday

#28
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):


(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 http://www.alh.net/newlisp/phpbb/viewtopic.php?t=2714">define-smacro in newLISP.
Get your Objective newLISP groove on.

newdep

#29
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))