Clojure-defining-a-function

提供:Dev Guides
2020年6月22日 (月) 22:42時点におけるMaintenance script (トーク | 投稿記録)による版 (Imported from text file)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先:案内検索

Clojure-関数の定義

関数は 'defn' マクロを使用して定義されます。 以下は、関数の定義の一般的な構文です。

構文

(defn functionname
   “optional documentation string”
   [arguments]
   (code block))

関数にはドキュメント文字列を含めることができます。これは、関数が実際に行うことを説明するのに適しています。

以下は関数の簡単な例です。

(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (def x 1)
   (def y 1.25)
   (def str1 "Hello")
   (println x)
   (println y)
   (println str1))
(Example)

上記の例では、関数の名前はExampleです。

出力

1
1.25
Hello