開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Clojure (プログラミング言語)
7つの言語 7つの世界 (Bruce A. Tate (著)、まつもとゆきひろ (監訳)、田和 勝 (翻訳)、オーム社)の第7章(Clojure)、7.2(1日目: ルークの修行)、セルフスタディ1日目を取り組んでみる。
セルフスタディ1日目.
コード(Emacs)
(defn big [st n] (if (> (count st) n) true false)) (defn collection-type [col] (let [type (class col)] (if (or (= type (class ())) (= type (class '(1)))) :list (if (= type (class {})) :map (if (= type (class [])) :vector :other))))) (def st1 "clojure") (println (big st1 6)) (println (big st1 7)) (println (big st1 8)) (println (collection-type '(1 2 3 4 5))) (println (collection-type (list 1 2 3 4 5))) (println (collection-type [1 2 3 4 5])) (println (collection-type {})) (println (collection-type {:a 1 :b 2})) (println (collection-type #{1 2 3 4 5})) (println (collection-type "Clojure"))
入出力結果(Terminal, REPL(Read, Eval, Print, Loop))
$ lein repl nREPL server started on port 55394 on host 127.0.0.1 - nrepl://127.0.0.1:55394 REPL-y 0.3.7, nREPL 0.2.12 Clojure 1.8.0 Java HotSpot(TM) 64-Bit Server VM 1.8.0_60-b27 Docs: (doc function-name-here) (find-doc "part-of-name-here") Source: (source function-name-here) Javadoc: (javadoc java-object-or-class-here) Exit: Control+D or (exit) or (quit) Results: Stored in vars *1, *2, *3, an exception in *e user=> (defn big [st n] (if (> (count st) n) true false)) (defn collection-type [col] (let [type (class col)] (if (or (= type (class ())) (= type (class '(1)))) :list (if (= type (class {})) :map (if (= type (class [])) :vector :other))))) (def st1 "clojure") (println (big st1 6)) (println (big st1 7)) (println (big st1 8)) (println (collection-type '(1 2 3 4 5))) (println (collection-type (list 1 2 3 4 5))) (println (collection-type [1 2 3 4 5])) (println (collection-type {})) (println (collection-type {:a 1 :b 2})) (println (collection-type #{1 2 3 4 5})) (println (collection-type "Clojure")) #'user/big user=> #_=> #_=> #_=> #_=> #_=> #_=> #_=> #_=> #_=> #'user/collection-type user=> #'user/st1 user=> user=> true nil user=> false nil user=> false nil user=> user=> :list nil user=> :list nil user=> :vector nil user=> :map nil user=> :map nil user=> :other nil user=> :other nil user=> user=> user=> Bye for now! $
0 コメント:
コメントを投稿