Clojure

関数型に関する紹介資料を作成するかも

社内の部門向けに資料を作成するかも。・関数型の特徴 ・実例 ・MapReduceなどのトレンド等を紹介できればと考えています。

関数型言語の特徴 まとめ

Javaのコードを Scala、Clojure、Lisp、Haskellで書いてみたけど、 基本的には、リスト作成の部分で差があるけど リストを操作する部分では、map関数やreduceもしくはfoldをつかって Javaで1メソッドとしていた部分を1行の関数呼び出しに 節約できた。書い…

関数型言語の特徴 Clojure版

user=> (def data '( {:firstName "hiroshi" :lastName "tanaka" :age 30} {:firstName "satoshi" :lastName "sato" :age 40} {:firstName "hanako" :lastName "suzuki" :age 20})) #'user/data user=> user=> (map :firstName data) ("hiroshi" "satoshi" "…

関数型言語の特徴 Clojure版(練習)

Clojureだとどう書くんだろう??? gen-classって構文でクラス定義するようだけど プログラミングClojureには載ってないらしい・・・誰か教えてつかぁさい! Clojure 1.1.0-alpha-SNAPSHOT user=> (def data '( {:firstName "hiroshi" :lastName "tanaka" :…

Clojureでのsetf、setqってどうやるの?

Clojureにはsetf、setqが無いようだけど 例えば、こんなコード CL-USER> (let ((lst '(a b (c d)))) (reverse lst)) ((C D) B A) これはClojureでもletが使えるからいいとして こんなコード CL-USER> (setf lst '(a b (c d))) (A B (C D)) CL-USER> (reverse…

JavaのStackOverflowError調査

Clojureとかのカラミで、Javaでどれだけ再帰できるか試してみた。JVMのパラメタは以下の通り。 -Xms40m -Xmx256m 実行したJavaコードは以下の通り。 クラスメソッドとインスタンスメソッドそれぞれで調査。 Javaだとメソッド呼び出しの度にスタックフレーム…

対話によるCommon Lisp 13.37(途中メモ)

テキストを見ての写経中だけど 途中でわけわかんなくなった (defclass !object () ((id :accessor id! :initform (new-id)))) (defvar *new-id* 0) (defun new-id () (incf *new-id*)) (defun eq! (!x !y) (= (id! !x) (id! !y))) (setf !x (make-instance '…

対話によるCommon Lisp 13.17-19

CL-USER> (make-symbol "RIE") #:RIE CL-USER> (intern "DOCTOR") DOCTOR NIL CL-USER> (symbol-name 'doctor) "DOCTOR" CL-USER> (symbol-value 'pi) 3.1415926535897932385L0 CL-USER> (symbol-function 'first) #<SYSTEM-FUNCTION FIRST> CL-USER> (symbol-plist 'x) NIL CL-USER</system-function>…

対話によるCommon Lisp 13.2-14

CL-USER> (defclass !object () ((id :accessor id! :initform (new-id)))) #<STANDARD-CLASS !OBJECT> CL-USER> (defvar *new-id* 0) *NEW-ID* CL-USER> (defun new-id () (incf *new-id*)) NEW-ID CL-USER> (defun eq! (!x !y) (= (id! !x) (id! !y))) EQ! CL-USER> (setf !x (make</standard-class>…

対話によるCommon Lisp 11.30-35

CL-USER> (defstruct person name age hobby) PERSON CL-USER> (setf p (make-person :name '1taro :age 20 :hobby '(wp))) #S(PERSON :NAME 1TARO :AGE 20 :HOBBY (WP)) CL-USER> (vectorp p) NIL CL-USER> (person-p p) T CL-USER> (person-name p) 1TARO …

対話によるCommon Lisp 11.12-13

CL-USER> (setf table (make-hash-table)) #S(HASH-TABLE :TEST EXT:FASTHASH-EQL) CL-USER> (setf (gethash 'zero table) 0) 0 CL-USER> (gethash 'zero table) 0 T CL-USER>

対話によるCommon Lisp 10.29

(loop initially (setf n 5) (setf s 0) for i from 1 to n do (incf s (* i i)) finally (return s)) 例外になった、なぜ?

対話によるCommon Lisp 10.21

CL-USER> (defun add-lesson () (progn (setf x (read)) (setf y (read)) (setf sum (+ x y)) (format t "~s + ~s = ~s" x y sum) sum)) ADD-LESSON CL-USER> (add-lesson) 20 30 20 + 30 = 50 50 CL-USER>

対話によるCommon Lisp 10.4-12

CL-USER> (setf x '(10 20 30)) (10 20 30) CL-USER> (setf (first x) 'ten) TEN CL-USER> x (TEN 20 30) CL-USER> (setf (rest x) '(nis)) (NIS) CL-USER> x (TEN NIS) CL-USER> (setf x (cons 'table x)) (TABLE TEN NIS) CL-USER> (push 'table x) (TABLE…

対話によるCommon Lisp 12.22

CL-USER> (defgeneric perimeter (x)) #<STANDARD-GENERIC-FUNCTION PERIMETER> CL-USER> (defclass triangle () ((edge1 :accessor triangle-edge1 :initarg :edge1) (edge2 :accessor triangle-edge2 :initarg :edge2) (edge3 :accessor triangle-edge3 :initarg :edge3))) #<STANDARD-CLASS TRIANGLE> CL-USER> (defmeth…</standard-class></standard-generic-function>

対話によるCommon Lisp 7.10

CL-USER> (defun compute (form) (if (atom form) form (case (second form) (+ (+ (compute (first form)) (compute (third form)))) (- (- (compute (first form)) (compute (third form)))) (* (* (compute (first form)) (compute (third form)))) (/ (/…

対話によるCommon Lisp 7.9

CL-USER> (defun color (fruit) (case fruit ((apple) 'red) ((banana lemon) 'yellow) ((peach) 'pink) (otherwise "I don't Know."))) COLOR CL-USER> (color 'lemon) YELLOW CL-USER> (color 'berry) "I don't Know." CL-USER> user=> (doc case) java.la…

対話によるCommon Lisp 7.6

CL-USER> (defun compute (form) (cond ((atom form) form) ((eql (second form) '+) (+ (compute (first form)) (compute (third form)))) ((eql (second form) '-) (- (compute (first form)) (compute (third form)))) ((eql (second form) '*) (* (compu…

対話によるCommon Lisp 9.45

CL-USER> (funcall #'+ '1 '2 '3 '4) 10 CL-USER> (apply #'+ '(1 2 3 4)) 10 CL-USER> (funcall #'cons 'a '(b c)) (A B C) CL-USER> (apply #'cons '(a (b c))) (A B C) CL-USER>

対話によるCommon Lisp 9.28-33

CL-USER> (member-if #'evenp '(3 5 6 9 10)) (6 9 10) CL-USER> (find-if #'evenp '(3 5 6 9 10)) 6 CL-USER> (remove-if #'evenp '(3 5 6 9 10)) (3 5 9) CL-USER> (mapcar #'sqrt '(1 4 25 36)) (1 2 5 6) CL-USER> (mapcar #'length '((a b) (c d e) () …

対話によるCommon Lisp 8.5-8

CL-USER> (defun root (x &optional n) (if (null n) (expt x 1/2) (expt x (/ 1 n)))) ROOT CL-USER> (root 16) 4 CL-USER> (root 64 3) 4 CL-USER> (defun root (x &optional (n 2)) (expt x (/ 1 n))) ROOT CL-USER> (root 16) 4 CL-USER> (root 64 3) 4 …

対話によるCommon Lisp 第Ⅰ部練習問題【1】

CL-USER> (setf x 'AKEBONO) AKEBONO CL-USER> (setf y '(WAKA TAKA)) (WAKA TAKA) CL-USER> '(cons x y) (CONS X Y) CL-USER> (cons 'x y) (X WAKA TAKA) CL-USER> (cons x 'y) (AKEBONO . Y) CL-USER> (cons x y) (AKEBONO WAKA TAKA) CL-USER> (list x y)…

対話によるCommon Lisp 7.36

CL-USER> (defun my_gcd (x y) (let ((r (mod x y))) (if (= r 0) y (my_gcd y r)))) MY_GCD CL-USER> (my_gcd 45 15) 15 CL-USER> (my_gcd 45 30) 15 CL-USER> gcdだとCommonLispのマクロと衝突しよった。 user=> (defn my_gcd [x y] (let [r (mod x y)] (i…

対話によるCommon Lisp 7.31-34

CL-USER> (let ((x 10) (y 20)) (+ x y)) 30 CL-USER> (let ((x 10) (y x)) (+ x y)) 例外発生した。。。 CL-USER> (let* ((x 10) (y x)) (+ x y)) 20 user=> (doc let) ------------------------- let Special Form Please see http://clojure.org/special_…

対話によるCommon Lisp 3.28

CL-USER> (setf apple 'apple) APPLE CL-USER> apple APPLE CL-USER> user=> (def apple 'apple) #'user/apple user=> apple apple

対話によるCommon Lisp 3.3

CL-USER> (read) common-lisp COMMON-LISP CL-USER> user=> (read) common-lisp common-lisp

対話によるCommon Lisp 2.29

CL-USER> (defun abs$ (x) (if (< x 0) (- x) x)) ABS$ CL-USER> (abs (- 2 5)) 3 user=> (defn abs$ [x] (if (< x 0) (- x) x)) #'user/abs$ user=> (abs$ (- 2 5)) 3

対話によるCommon Lisp 2.2

CL-USER> (defun square-sum (x y) (+ (expt x 2) (expt y 2))) SQUARE-SUM CL-USER> (square-sum 3 5) 34 CL-USER> user=> (defn pow-sum [x y] (+ (Math/pow x 2) (Math/pow y 2))) #'user/pow-sum user=> (pow-sum 3 5) 34.0

対話によるCommon Lisp 1.30

CL-USER> (remove 20 people :test #'<= :key #'second) ((RIE 19) (ERIKA 9)) CL-USER> user=> (remove 20 people :test #'<= :key #'second) java.lang.IllegalArgumentException: Wrong number of args passed to: core$remove (NO_SOURCE_FILE:0)

対話によるCommon Lisp 1.29

CL-USER> (setf people '((RIE 19) (TOM 22) (KEVIN 37) (ERIKA 9))) ((RIE 19) (TOM 22) (KEVIN 37) (ERIKA 9)) CL-USER> user=> (doc setf) java.lang.Exception: Unable to resolve var: setf in this context (NO_SOURCE_FILE :15) user=> (def people '…