Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "B. Pym" Newsgroups: comp.lang.lisp Subject: Re: A style question Date: Fri, 9 Aug 2024 08:53:24 -0000 (UTC) Organization: A noiseless patient Spider Lines: 80 Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Injection-Date: Fri, 09 Aug 2024 10:53:24 +0200 (CEST) Injection-Info: dont-email.me; posting-host="b56adfd1b06b773bd0a515961a3e0f95"; logging-data="704515"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/H91a0lIygR2WEi1xBflKF" User-Agent: XanaNews/1.18.1.6 Cancel-Lock: sha1:pHWilOHNdARDSXLHIwFl9VXUxHc= Bytes: 2440 B. Pym wrote: > Brian wrote: > > > > (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) > > > > Another friend of mine commenting on the same FizzBuzz thread supplied > > the following Python code. It certainly is concise: > > > > for i in xrange(1,101): > > print(str(i), "Fizz", "Buzz", "FizzBuzz")[(i%3==0)|(i%5==0)<<1] > > newLISP > > (define (fizz) > (define (any _test) (exists _test $args)) > (define (divis n) (= 0 (mod i n))) > (for (i 1 21) > (if (divis 3) (print 'fizz)) > (if (divis 5) (print 'buzz)) > (if (divis 7) (print 'zoom)) > (unless (any divis 3 5 7) (print i)) > (println))) > > (fizz) > > 1 > 2 > fizz > 4 > buzz > fizz > zoom > 8 > fizz > buzz > 11 > fizz > 13 > zoom > fizzbuzz > 16 > 17 > fizz > 19 > buzz > fizzzoom Another way: (define-macro (try-all) ;; Arguments are in $args or (args). (let (_successes nil) (dolist (_expr $args) (when (eval _expr) (++ _successes))) _successes)) (define (fizz) (define (divis n) (= 0 (mod i n))) (for (i 1 21) (unless (try-all (if (divis 3) (print 'fizz)) (if (divis 5) (print 'buzz)) (if (divis 7) (print 'zoom))) (print i)) (println)))