Lisp達のFizzBuzz(リスト生成&String.Join)

;CommonLisp (format t "~{~A~%~}" (loop for i […]

;CommonLisp (format t "~{~A~%~}" (loop for i […]

  • タグ:
  • タグはありません
;CommonLisp
(format t "~{~A~%~}" 
	(loop for i from 1 to 100 collect
		(let(
				(fizz (= 0 (mod i 3)))
				(buzz (= 0 (mod i 5))))
			(if (and fizz buzz)
				"Fizz Buzz"
			(if fizz
				"Fizz"
			(if buzz
				"Buzz"
				i))))))

;Scheme
(display (string-append(string-join
	(map(lambda(i)
		(let(
				(fizz (= 0 (mod i 3)))
				(buzz (= 0 (mod i 5))))
			(if (and fizz buzz)
				"Fizz Buzz"
			(if fizz
				"Fizz"
			(if buzz
				"Buzz"
				(number->string i))))))
		(iota 100 1))
	"\n")"\n"))

;Clojure
(require '[clojure.string :as str])

(println (str/join "\n"
	(for[i (range 1 (inc 100))]
		(let[
				fizz,(= 0 (mod i 3))
				buzz,(= 0 (mod i 5))]
			(if (and fizz buzz)
				"Fizz Buzz"
			(if fizz
				"Fizz"
			(if buzz
				"Buzz"
				i)))))))

#Python3
print("\n".join(
	[(lambda
		fizz=0==i%3,
		buzz=0==i%5:
			"Fizz Buzz" if fizz and buzz else
			"Fizz" if fizz else
			"Buzz" if buzz else
			str(i))()
		for i in range(1,100+1)]))