変形Fizz-Buzz問題
Posted feedbacks - Scheme
以前WiLiKi (http://practical-scheme.net/wiliki/wiliki.cgi?Scheme%3aFizzBuzz)に書いたものの焼き直しですが、hygienic macroで解いてみました。
ただ、素直に解いた方が短いです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | (define-syntax show-msg
(syntax-rules ()
((_ current v next)
(define-syntax current
(syntax-rules ()
((_) #t)
((_ elem . rest)
(begin
(print #`",|elem|:,v")
(next . rest))))))))
(define-syntax define-fizzbuzz
(syntax-rules (Fizz Buzz FizzBuzz)
((_ v) #t)
((_ current Fizz next rest ...)
(begin
(show-msg current "Fizz" next)
(define-fizzbuzz next rest ...)))
((_ current Buzz next rest ...)
(begin
(show-msg current "Buzz" next)
(define-fizzbuzz next rest ...)))
((_ current FizzBuzz next rest ...)
(begin
(show-msg current "FizzBuzz" next)
(define-fizzbuzz next rest ...)))
((_ current next rest ...)
(begin
(show-msg current "hoge" next)
(define-fizzbuzz next rest ...)))))
(define-fizzbuzz a1 a2 a3 Fizz a4 a5 Buzz a6 Fizz a7 a8 a9 Fizz a10 Buzz a11 a12 Fizz a13 a14 a15 FizzBuzz a1)
(a1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)
|
Scheme(Gauche)で。題意に満すのかどうかわかりませんが……。
1 2 3 4 5 6 7 8 | (use srfi-1)
(map (lambda (n)
(format #t "~a:~a\n" n (or (and (= (modulo n 15) 0) "FizzBuzz")
(and (= (modulo n 3) 0) "Fizz")
(and (= (modulo n 5) 0) "Buzz")
"hoge")))
(iota 20 1))
|
循環リストで。
1 2 3 4 5 6 7 | use srfi-1)
(define (fizzbuzz n)
(for-each (lambda (a b) (print a ":" b))
(iota n 1)
(circular-list 'hoge 'hoge 'Fizz 'hoge 'Buzz 'Fizz 'hoge 'hoge 'Fizz 'Buzz 'hoge 'Fizz 'hoge 'hoge 'FizzBuzz)))
(fizzbuzz 20)
|
言語組込みの条件分岐を使わず、ラムダ算法による真偽値表現を使ってみました。
1 2 3 4 5 6 7 8 9 10 11 | (use srfi-1)
(use srfi-42)
(define (fizzbuzz n)
(define (t x y) x)
(define (f x y) y)
(do-ec (:parallel (: i n)
(:list mod3 (circular-list f f t))
(:list mod5 (circular-list f f f f t)))
(format #t "~2d: ~a\n" (+ i 1)
(mod3 (mod5 "FizzBuzz" "Fizz") (mod5 "Buzz" "hoge")))))
|


raynstard
#3758()
Rating0/2=0.00
[ reply ]