A style question
- From: job-271842874@xxxxxxxxxxxxxx
- Date: Tue, 27 Feb 2007 20:12:33 -0500
A friend passed on an article regarding the difficulty job candidates had in producing even simple programs (simple as in should take a minute or less). One example was a program to print the numbers 1 to 100 except that "Fizz" should be substituted for numbers divisible by 3, "Buzz" should be substituted for numbers divisible by 5, and "FizzBuzz" should be substituted for numbers divisible by both 3 and 5.
So, having received my "ANSI Common Lisp" and "Practical Common Lisp" books two days ago (still waiting for "Structure and Interpretation of Computer Programs") and having a *couple hours* of Lisp under my belt, I cranked out a Lisp version. It seems a bit clunky, so I thought I'd see if anyone had suggestions for improvements.
Here's a Ruby version for comparison:
def fizz_buzz n
1.upto(n) do |i|
print "Fizz" if fizz = (i % 3) == 0
print "Buzz" if buzz = (i % 5) == 0
puts fizz || buzz ? "" : i
end
end
fizz_buzz 100
and the Lisp version
(defun fizz-buzz (n)
(do ((i 1 (+ i 1)))
((> i n))
(let
((fizz (= 0 (mod i 3)))
(buzz (= 0 (mod i 5))))
(if fizz (format t "Fizz"))
(if buzz (format t "Buzz"))
(format t "~A~%"
(if (or fizz buzz) "" i)))))
(fizz-buzz 100)
By the way, it seems more folks recommend starting with "Practical Common Lisp" instead of "ANSI Common Lisp" (when both are mentioned), but being a newbie myself, it seems that the latter is a better introduction to the language.
Brian
.
- Follow-Ups:
- Re: A style question
- From: job-271842874
- Re: A style question
- From: Wade Humeniuk
- Re: A style question
- From: Paul Wallich
- Re: A style question
- From: Lars Rune Nøstdal
- Re: A style question
- From: Frank Buss
- Re: A style question
- Prev by Date: Re: -2 is not a real number?!?
- Next by Date: Re: A style question
- Previous by thread: sexp-aware theory of patches and "meta common lisp"
- Next by thread: Re: A style question
- Index(es):
Relevant Pages
|