Created
February 1, 2015 01:51
-
-
Save atsuya046/4e98621f11ae9c0a176b to your computer and use it in GitHub Desktop.
clj_grammar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; ====== 値の束縛 ======== | |
; シンボルに値を束縛する(いわゆる代入) | |
(def x 3) | |
(def y (+ x 1)) | |
; ====== 関数 ======== | |
; 関数を定義する | |
(defn hello-clojure [] | |
(println "Hello Clojure")) | |
; 関数を評価する | |
(hello-clojure) | |
; 引数を利用する関数を定義する | |
(defn sumup [a b c] | |
(+ a b c)) | |
; 関数に引数を与えて評価する | |
(sumup 1 2 3) | |
; 関数の戻り値は最後の行 | |
(defn hoge [] | |
(+ 1 2) | |
(+ 3 4)) ;; 3+4の計算結果7が返却される | |
; 可変の引数を受け取る関数を定義する | |
(defn print-args [& args] | |
(println args)) | |
(print-args 1 2 3) ;; (1 2 3)と出力される | |
; ======= 無名関数 ====== | |
; 無名関数の書き方 | |
(fn [a b c] | |
(* a b c)) ;; これだけだと無名関数が定義されるだけ | |
; 無名関数の呼び出し | |
((fn [a b c] (* a b c)) 2 3 4) ;; 2*3*4の計算結果24が返される | |
; 変数束縛と関数の定義(以下の2行は同じ意味) | |
(def inc3 (fn [x] (+ x 3))) ;; inc3に無名関数を束縛 | |
(defn inc3 [x] (+ x 3)) ; inc3を定義 | |
; 無名関数の簡略表現 | |
; (fn [a b c] (* a b c)) <=> #(* %1 %2 %3) | |
(#(* %1 %2 %3) 2 3 4) ;; 2*3*4の計算結果24が返される | |
; ======= scope ======= | |
; 狭い範囲で束縛 | |
(let [x 3 | |
y 4] | |
(* x y)) ;; 3*4の計算結果12が返される | |
(println x y) ;; ここからはletで束縛したx, yにはアクセスできない | |
; namespaceで変数を閉じ込める | |
(ns abc.core ;; abc.coreというnamespaceを作成 | |
(:require [clojure.end :as edn])) ;; namespaceがclojure.ednのmoduleをednという名前で使用する |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment