な、なんですとー。
1つの言語に囲い込まれるリスク @ 2008年01月 @ ratio - rational - irrational @ IDMarcリリースおめでとう!
というわけで覗いてみた。
ちょwww これ、Lisp Fundamentalist ほど拒絶反応強そう。でも私はちょっと萌えちゃった。
以下、Tutorialからぶっとび箇所だけぴっくあっぷ。
defvar は =
まずはこれ。
arc> (= foo 13) 13 arc> foo 13
defun は def
Scheme方式の(define (funname arg) (...)
でなくて(defvar funname (arg) (...)
です。mzschemeで実装されてるくせにぃ。
arc> (def average (x y) (/ (+ x y) 2)) #<procedure: average> arc> (average 2 4) 3
schemeの方が好きな私としては、変数の方もdef
でいいんじゃ、とおもったりもするのですが、変数への代入と関数定義を分けてあるおかげで、こういうことも出来ます。
arc> x (a b) arc> (= (car x) 'z) z arc> x (z b)
lambda はλ、じゃなくてfn
これは納得がいくところ。なんたってよく使いますから。
arc> ((fn (x y) (/ (+ x y) 2)) 2 4) 3
データ構造 = 関数
このあたり機能としてはLispの真骨頂。ですがこれってLispにみえねえぇ
arc> ("foo" 0) #\f arc> ("foo" 1) ; 弾追補 #\o
うにこーどのあつかいは?
ローカル変数はwithで
このようにletもあるのですが、
arc> (let x 1 (+ x (* x 2))) 3
複数変数を使いたければwithを使います。
arc> (with (x 3 y 4) (sqrt (+ (expt x 2) (expt y 2)))) 5
ifの重ね打ち
(if a b c d e) ; 以下と同じ ; (if a ; b ; (if c ; d ; e))
Graham御大って、括弧萌えのネスト嫌い?
andとorはショートサーキット
これはわかりやすい
arc> (and nil (pr "you'll never see this")) nil
否定はnotじゃなくてno
なんで!
にしなかったんだろ
arc> (def mylen (xs) (if (no xs) 0 (+ 1 (mylen (cdr xs))))) #<procedure: mylen> arc> (mylen nil) 0 arc> (mylen '(a b)) 2
== は is
これはなるほどかも。==
よりはわかりやすい。
arc> (is 'a 'a) t arc> (is "foo" "foo") t
ただし、リストの内容まで比較する場合はiso (isomorphicの略)
arc> (iso (list 'a) (list 'a)) t
for/each/while/repeat
これはふつうのLL使いにはふつう。
arc> (for i 1 10 (pr i " ")) 1 2 3 4 5 6 7 8 9 10 nil arc> (each x '(a b c d e) (pr x " ")) a b c d e nil arc> (let x 10 (while (> x 5) (= x (- x 1)) (pr x))) arc> (repeat 5 (pr "la ")) la la la la la nil
ちょっと賢いmap
ふつうは
arc> (map (fn (x) (+ x 10)) '(1 2 3)) (11 12 13 . nil)
ですが、こんなこともできます。
arc> (map + '(1 2 3 4) '(100 200 300)) (101 202 303)
で、これ。
arc> (map [+ _ 10] '(1 2 3)) (11 12 13 . nil)
それってどこのperl?
keepでgrep
これはセンスがいい。
arc>> (keep odd '(1 2 3 4 5 6 7)) (1 3 5 7)
でもこれは略し過ぎ
arc> (rem odd '(1 2 3 4 5 6)) (2 4 6)
だって
arc> (keep ~odd '(1 2 3 4 5 6)) (2 4 6)
で事足りるはずだもん。remってbasicだとコメントになっちゃうよ〜
ふつうにhashがあるよん
これは大歓迎!
arc> (= airports (table)) #hash() arc> (= (airports "Boston") 'bos) bos arc> (let h (listtab '((x 1) (y 2))) (h 'y)) 2 arc> (let h (obj x 1 y 2) (h 'y)) 2
たいぷきゃすとおぉ
きもい。でも便利そう。
arc> (coerce "foo" 'cons) (#\f #\o #\o) arc> (coerce "99" 'int) 99 arc> (coerce "99" 'int 16) 153
push して pop
arc> (= x '(c a b)) (c a b) arc> (pop x) c arc> x (a b) arc> (push 'f x) (f a b) arc> x (f a b)
マックマクロ
一番Grahamらしいと思ったのがこれ。roがろったいないのですね。
(mac when (test . body) `(if ,test (do ,@body)))
らんらんる〜
以上、駆け足でarcを見てきましたが、なまじlisp/schemeを知っているとキモく見えますが、はじめからこれでいったら結構気持ち良く使えるのではないかと思っちゃった私は駱駝脳なのでしょうねえ。listがarrayっぽく使えて(e.g. push/pop)、hashがビルトインというのはより「ポストモダン言語」っぽい一方、car
とかcdr
とかは、「古き佳きlisp」のままですし。
でも、一番外側の()をどう見るかが、arcを気に入るかどうかの酒井鴨。
'(dan the arc newbie)
http://www.lingr.com/room/gauche/archives/2008/02/01#msg-25178946
$ gosh
gosh> (define fn lambda)
fn
gosh> (map (fn (x) (+ x 10)) '(1 2 3))
(11 12 13)
gosh> (map + '(1 2 3 4) '(100 200 300))
(101 202 303)