1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

clojureでFizzBuzz

Posted at

文法も何もわからなかったので大変でした…(ヽ´ω`)

src/fizzbuzz/core.clj
(ns fizzbuzz.core
  (:gen-class))

(defn fizzbuzz
  "引数が3の倍数ならFizz、5の倍数ならBuzz、
   3と5の公倍数ならFizzBuzzに変換する"
  [n]
  (cond
    (= (rem n 15) 0) "FizzBuzz"
    (= (rem n 3) 0) "Fizz"
    (= (rem n 5) 0) "Buzz"
    :else (str n)))

(defn -main
  "メイン"
  [& args]
  (println (clojure.string/join " " (map fizzbuzz (range 1 101)))))

##覚えたこと
関数を定義するにはdefnを使う

(defn [関数名] [関数の説明] [引数リスト] [戻り値])

関数は頭から評価されていくのか、fizzbuzz関数をmain関数の後ろに置いたらエラーになってしまいました。

比較は=

剰余はrem

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?