Two minute challenge

Started by cormullion, December 22, 2008, 05:50:59 AM

Previous topic - Next topic

m i c h a e l

#30
Here comes my unavoidable FOOP version:


(define (FizzBuzz:FizzBuzz n) (when (zero? (% n 15)) "FizzBuzz"))
(define (Fizz:Fizz n) (when (zero? (% n 3)) "Fizz"))
(define (Buzz:Buzz n) (when (zero? (% n 5)) "Buzz"))

((define (fizz-buzz)
(for (n 1 100)
(println (or (FizzBuzz n) (Fizz n) (Buzz n) n))
)
))


How long did it take me? My initial try took about eight minutes. I guess that means I'm not much of a programmer ;-)



m i c h a e l

cormullion

#31
Very clever, Alessandro!



m i c h a e l (aka FOOP-erman): an elegant masterpiece is bound to take a few more minutes. I've never seen that ((define ..)) form before... ingenious!

ale870

#32
michel your solution is great! I like it (and I'm studying it)

Thank you for your contribution!
--

Kazimir Majorinc

#33
I offer to Newlisp users and sympathizers the program named "Parasite." It works for me this moment.


(println (slice (read-file (append "http://www.alh.net/newlisp/phpbb/"
                                   "viewtopic.php?t=2582&postdays=0"
                                   "&postorder=asc&start=0"))
                105417
                (pow 2 9)))


If it doesn't work, this one should:


(set 'z (read-file (append "http://www.alh.net/newlisp/phpbb/"
                           "viewtopic.php?t=2582&postdays=0"
                           "&postorder=asc&start=0")))

(println (slice z
                (find "1, 2, Fizz, 4" z)
                (pow 2 9)))




Happy new year and good luck to everyone.
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

m i c h a e l

#34
Thank you, cormullion and Alessandro. Your kind words always inspire.



Kazimir, thanks for another great, unconventional solution. That's one of the things I like best about newLISP and this community: many different ways of looking at programming—and newLISP gracefully accommodating them all.


Quote from: "cormullion" I've never seen that ((define ..)) form before... ingenious!


I'm sure code like this would be discouraged in more formal settings, but I find its pure elegance hard to resist ;-)


Quote from: "Alessandro" I like it (and I'm studying it)


This could also be de-FOOPed by replacing the constructors with functions:


(define (fizzbuzz n) (when (zero? (% n 15)) "FizzBuzz"))
(define (fizz n) (when (zero? (% n 3)) "Fizz"))
(define (buzz n) (when (zero? (% n 5)) "Buzz"))

((define (fizz-buzz)
(for (n 1 100)
(println (or (fizzbuzz n) (fizz n) (buzz n) n))
)
))


m i c h a e l