user=> (+ 7654 1234)
8888
REPLを使用して、7654と1234の合計を計算します。
user=> (+ 7654 1234)
8888
次の代数式をクロージャー式に書き直します: ( 7 + 3 * 4 + 5 ) / 10
。
(/ (+ 7 (* 3 4) 5) 10)
REPLドキュメント関数を使い、rem関数とmod関数のドキュメントを見ます。ドキュメントに基づいて、提供された式の結果を比較します。
user=> (doc rem)
clojure.core/rem
([num div])
remainder of dividing numerator by denominator.
nil
user=> (doc mod)
clojure.core/mod
([num div])
Modulus of num and div. Truncates toward negative infinity.
nil
find-docを使用して、最新のREPL例外のスタックトレースを出力する関数を見つけます。
pst
引数を取らずに「Hello」を出力する関数greet
を定義します。
(defn greet []
(println "Hello"))
fn
と#()
を使用してdef
でgreetを再定義します。
;; using fn
(def greet
(fn [] (println "Hello")))
;; using #()
(def greet
#(println "Hello"))
関数greeting
を定義します…
(defn greeting
([] (greeting "Hello" "World"))
([x] (greeting "Hello" x))
([x y] (str x ", " y "!")))
関数do-nothing
を定義します…
(defn do-nothing [x] x)
関数always-thing
を定義します…
(defn always-thing [& xs] 100)
関数make-thingy
を定義します…
(defn make-thingy [x]
(fn [& args] x))
関数triplicate
を定義します…
(defn triplicate [f]
(f) (f) (f))
関数opposite
を定義します…
(defn opposite [f]
(fn [& args] (not (apply f args))))
関数triplicate2
を定義します…
(defn triplicate2 [f & args]
(triplicate (fn [] (apply f args))))
java.lang.Math
クラスを使用して…
user=> (Math/cos Math/PI)
-1.0
user=> (+ (Math/pow (Math/sin 0.2) 2)
(Math/pow (Math/cos 0.2) 2))
1.0
HTTP URLを文字列として取る関数を定義します…
(defn http-get [url]
(slurp
(.openStream
(java.net.URL. url))))
(defn http-get [url]
(slurp url))
関数one-less-arg
を定義します。
(defn one-less-arg [f x]
(fn [& args] (apply f x args)))
関数two-fns
を定義します。
(defn two-fns [f g]
(fn [x] (f (g x))))