2012/12/29 Nikita Beloglazov <[email protected]>
>
> Hi
>
> I'd change your fizzy function so it returns a string instead of printing it.
> This way it will be pure function and more functional-like. In doseq you'll
> need (printlng (fuzzy x)) instead of
> (fuzzy x).
>
> Nikita Beloglazov
>
> On Saturday, December 29, 2012 3:35:38 PM UTC+3, Sean Chalmers wrote:
>>
>> Greetings all!
>>
>> I'm just starting out in the so far wonderful world of Clojure and to help
>> me get started I had a crack at one of my favourites, the FizzBuzz program.
>> For anyone that isn't familiar with FizzBuzz, it is designed to count from 1
>> to N and take the following actions when certain conditions are met:
>>
>> When the remainder of i divided by 3 is 0 print "Fizz"
>> When the remainder of i divided by 5 is 0 print "Buzz"
>> When both the former are true of i print "FizzBuzz"
>>
>> I crafted the following as a solution and I would really appreciate some
>> more experienced Clojurians casting their eye over it and letting me know if
>> what I've done is in the style and spirit of Clojure. Also this is my first
>> functional language so any feedback on that would be awesome too. :)
>>
>> I'm aware it's only a teeeeeeeensy piece of code so not terribly indicative
>> of the hilarity that might ensue on a larger project but all the same.
>> Enough of my blathering here is the meaty bit:
>>
>> (defn zero-remainder? [x y]
>> (zero? (rem x y)))
>>
>> (defn fizzy [x]
>> (let [fizz (zero-remainder? x 3) buzz (zero-remainder? x 5)]
>> (if (and fizz buzz)
>> (println "FizzBuzz: " x)
>> (if buzz
>> (println "Buzz: " x)
>> (if fizz
>> (println "Fizz: " x))))))
What about:
(defn fizz [x]
(str
(when (zero-reminder? x 3) "Fizz")
(when (zero-reminder? x 5) "Buzz")
":" x))
Cheers,
--
Laurent
>>
>>
>> (doseq [x (range 1 25)]
>> (fizzy x))
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en