by Peter Norvig
$B$3$l$O(B Lisp $B%W%m%0%i%^$N$?$a$N4JC1$J(B Python $B35@b$G$"$k!#(B
($B$O$+$i$:$b!"(BPython $B%W%m%0%i%^$K$H$C$F$b$3$N%Z!<%8$O(B Lisp $B$r3X$V$N$K$h$$$=$&$@!#(B)
$B4pK\E*$K$O!"(BPython $B$O!VEAE}E*$J!W9=J8(B (Lisp $B;H$$$,(B "infix ($BCfCV5-K!(B)" $B$H$+(B "m-lisp" $B$H$+8F$V$b$N(B)
$B$r$b$D(B Lisp $B$NJ}8@$H;W$o$l$F$$$k!#(BComp.lang.python $B$N$"$k%a%C%;!<%8$K$h$l$P(B
$B!V<+J,$O(B Python $B$GM7$V$h$&$K$J$k$^$G!"$J$<(B LISP $B$,$=$s$J$K$$$$%"%$%G%"$J$N$+(B
$B$o$+$i$J$+$C$?!W$H$N$3$H$G!"(BPython $B$O(B $B%^%/%m(B $B$r$N$>$/(B
Lisp $B$N
$B$o$?$7$,(B Python $B$KL\$r8~$1$?$N$O!"(B
Russell & Norvig $B$N(B AI $B652J=q(B $B$K$"$k(B
Lisp $B%3!<%I(B $B$r(B
Lisp $B$+$i(B Java $B$K0\?"$7$h$&$H9M$($F$$$?$+$i$@$C$?!#2??M$+$N65;U$H3X@8$,(B Java $B$rK>$s$G$$$?!#(B
$B$=$NM}M3$O(B:
- $BB>$N%/%i%9$G$b$C$H$b$J$8$s$G$$$?8@8l$@$C$?$+$i!#(B
- $B%0%i%U%#%+%k$J%"%W%j%1!<%7%g%s$,M_$7$+$C$?$i$7$$!#(B
- $B>/?tGI$G$O$"$C$?$,!"%V%i%&%6>e$N%"%W%l%C%H$rK>$s$@?M$b$$$?!#(B
- $B2??M$+$O$?$@C1$K(B Lisp $B$NJ8K!$K47$l$k$3$H$,$G$-$J$+$C$?!#(B
$BH`$i$,
$B$7$+$7!"(BJava $B$N%3!<%I$G$3$l$iAv$i$;$k$H$$$&(B
$B;n$_(B $B$O(B
$B$"$^$j@.8y$7$?$H$O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!G=(B |
$B$9$Y$F$O%*%V%8%'%/%H$G$"$k(B |
$B$O$$(B |
$B$O$$(B |
$B7?$O%*%V%8%'%/%H$,J]M-$7!"(B $BJQ?t$,J]M-$9$k$N$G$O$J$$(B |
$B$O$$(B |
$B$O$$(B |
$B%j%9%H$K$$$m$s$J$b$N$rF~$l$i$l$k(B |
$B$O$$(B ($BO"7k%j%9%H(B $B$HG[Ns(B) |
$B$O$$(B ($BG[Ns(B) |
$BJ#?t$N%Q%i%@%$%`$r%5%]!<%H$7$F$$$k(B |
$B$O$$(B: $B4X?t7?!"Generic |
$B$O$$(B: $B4X?t7?!" |
$B5-214IM}(B |
$B<+F0%,!<%Y%8%3%l%/%7%g%s(B |
$B<+F0%,!<%Y%8%3%l%/%7%g%s(B |
$B%Q%C%1!<%8(B/$B%b%8%e!<%k(B |
$B;H$$$E$i$$(B |
$B;H$$$d$9$$(B |
$B%*%V%8%'%/%H$d%/%i%9$N<+8J5-=R(B
| $B6/NO(B
| $B6/NO(B
|
$B%a%?%W%m%0%i%_%s%0$N$?$a$N%^%/%m(B |
$B%Q%o%U%k$J%^%/%m40Hw(B |
$B%^%/%m$J$7(B
|
$BBPOCE*$J!VF~NO(B-$BI>2A(B-$BI=<((B (Read-eval-print)$B!W%k!<%W(B |
> (string-append "hello" " " "world")
"hello world" |
>>> ' '.join(['hello', 'world'])
'hello world' |
$B4J7i$+$DI=8=NO$"$k8@8l(B |
(defun transpose (m)
(apply #'mapcar #'list m))
> (transpose '((1 2 3) (4 5 6)))
((1 4) (2 5) (3 6))
|
def transpose (m):
zip(*m)
>>> transpose([[1,2,3], [4,5,6]])
[(1, 4), (2, 5), (3, 6)]
|
$B%W%i%C%H%U%)!<%`4V$G$N0\?"@-(B |
Windows, Mac, Unix, Linux | Windows, Mac, Unix, Linux
|
$B$B$?$/$5$s(B | $B$R$H$D(B
| |
$B3+H/%b%G%k(B | $B>&MQ$*$h$S%*!<%W%s%=!<%9(B | $B%*!<%W%s%=!<%9(B
|
$B8zN((B |
C++ $B$h$j(B 1$B!A(B2$BG\CY$$!#(B
| C++ $B$h$j(B 2$B!A(B100$BG\CY$$!#(B
|
GUI, Web $B$J$I$N%i%$%V%i%j(B |
$BI8=`$J$7(B
| GUI, Web $B%i%$%V%i%j$OI8=`(B
|
$B%a%=%C%I(B $B%a%=%C%I$BF0E*(B, $B=q<0$O(B (meth obj arg)
$B$BJ#?t%a%=%C%I(B
| $BF0E*(B, $B=q<0$O(B obj.meth(arg)
$B$B%/%i%9$4$H(B
| |
$B%G!<%?7?(B |
Lisp $B$K$*$1$k%G!<%?7?(B |
Python $B$K$*$1$k%G!<%?7?(B |
Integer ($B@0?t(B)
Bignum ($BBg$-$J?t(B)
Float ($BIbF0>.?tE@?t(B)
Complex ($BJ#AG?t(B)
String ($BJ8;zNs(B)
Symbol ($B%7%s%\%k(B)
Hashtable/Dictionary ($B%O%C%7%e(B/$B<-=q(B)
Function ($B4X?t(B)
Class ($B%/%i%9(B)
Instance ($B%$%s%9%?%s%9(B)
Stream ($B%9%H%j!<%`(B)
Boolean ($B??M}CM(B)
Empty Sequence ($B6u%j%9%H(B)
$BCM$N$J$$>l9g(B
Lisp List ($BO"7k%j%9%H(B)
Python List ($BD4@02DG=$JG[Ns(B)
$B$=$NB>(B
|
42
100000000000000000
12.34
#C(1, 2)
"hello"
hello
(make-hash-table)
(lambda (x) (+ x x))
(defclass stack ...)
(make 'stack)
(open "file")
t, nil
(), #() linked list, array
nil
(1 2.0 "three")
(make-arrary 3 :adjustable t
:initial-contents '(1 2 3))
$B$?$/$5$s(B ($B8@8l$NCf$K(B)
|
42
100000000000000000
12.34
1 + 2J
"hello" $B$^$?$O(B'hello' ## $BJQ99IT2D(B
'hello'
{}
lambda x: x + x
class Stack: ...
Stack()
open("file")
1, 0
(), [] tuple, array
None
(1, (2.0, ("three", None)))
[1, 2.0, "three"]
$B$?$/$5$s(B ($B%i%$%V%i%jCf$K(B)
|
$B@)8f9=B$(B |
Lisp $B$K$*$1$k@)8f9=B$(B |
Python $B$K$*$1$k@)8f9=B$(B |
$B<0$H%9%F!<%H%a%s%H(B |
$B$9$Y$F$O<0$G$"$k(B |
$B<0$H%9%F!<%H%a%s%H$rJ,$1$k(B |
$B56$r$"$i$o$9CM(B |
nil $B$,M#0l$NCM$G$"$k(B |
None, 0, '', [ ], {} $B$O$I$l$b56$r$"$i$o$9(B |
$B4X?t8F$S=P$7(B |
(func x y z) |
func(x,y,z) |
$B>r7oH=CG(B |
(if x y z) |
if x: y
else: z |
While $B%k!<%W(B |
(loop while (test) do (f)) |
while test(): f() |
$B$=$NB>$N%k!<%W(B |
(dotimes (i n) (f i))
(loop for x in s do (f x))
(loop for (name addr salary) in db do ...) |
for i in range(n): f(i)
for x in s: f(x) ## $B$I$s$J%7!<%1%s%9$K$b2DG=(B
for (name, addr, salary) in db: ... |
$BBeF~(B |
(setq x y)
(psetq x 1 y 2)
(rotatef x y)
(setf (slot x) y)
(values 1 2 3) $B%9%?%C%/>e(B
(multiple-value-setq (x y) (values 1 2)) |
x = y
x, y = 1, 2
x, y = y, x
x.slot = y
(1, 2, 3) $B%R!<%WCf$N%a%b%j$r;H$&(B
x, y = 1, 2 |
$BNc30(B |
(assert (/= denom 0))
(unwind-protect (attempt) (recovery))
.AN
(catch 'ball ... (throw 'ball))
|
assert denom != 0, "denom != 0"
try: attempt()
finally: recovery()
try: ...; raise 'ball'
except 'ball': ... |
$B$=$NB>$N@)8f9=B$(B |
case, etypecase, cond, with-open-file $B$J$I(B |
$B$3$l0J30$K@)8f9=B$$O$J$$(B |
$B;z6g9=B$(B |
Lisp $B$K$*$1$k;z6g9=B$(B |
Python $B$K$*$1$k;z6g9=B$(B |
$B%3%a%s%H(B |
;; $B%;%_%3%m%s$+$i9TKv$^$G(B |
## $B%O%C%7%e%^!<%/$+$i9TKv$^$G(B |
$B6h@Z$jJ8;z(B |
$B%+%C%3$G<0$r6h@Z$k(B:
(defun fact (n)
(if (<= n 1) 1
(* n (fact (- n 1)))))
|
$B%$%s%G%s%H$G<0$r6h@Z$k(B:
def fact (n):
if n <= 1: return 1
else: return n * fact(n — 1)
|
$B9b3,4X?t(B |
Lisp $B$K$*$1$k9b3,4X?t(B |
Python $B$K$*$1$k9b3,4X?t(B |
$B4X?t$NE,MQ(B
$B<0$NI>2A(B
$B%9%F!<%H%a%s%H$N$B%U%!%$%k$N%m!<%I(B |
(apply fn args)
(eval '(+ 2 2)) => 4
(eval '(dolist (x list) (f x)))
(load "file.lisp") |
apply(fn, args) $B$^$?$O(B fn(*args)
eval("2+2") => 4
exec("for x in list: f(x)")
execfile("file.py") $B$^$?$O(B import file |
$B%7!<%1%s%9MQ$N4X?t(B |
(mapcar length '("one" (2 3))) $B"M(B (3 2)
(reduce #'+ numbers)
(remove-if-not #'evenp numbers)
(reduce #'min numbers) |
map(len, ["one", [2, 3]]) $B"M(B [3, 2]
$B$^$?$O(B [len(x) for x in ["one", [2, 3]]]
reduce(operator.add, numbers)
filter(lambda x: x%2 == 0, numbers)
$B$^$?$O(B [x for x in numbers if x%2 == 0]
min(numbers) |
$B$=$NB>$N9b3,4X?t(B |
some, every, count-if $B$J$I(B
:test, :key $B$J$I$N%-!<%o!<%I(B |
$BAH9~$_$G$O$3$l0J30$N9b3,4X?t$O$J$$(B
map/reduce/filter $B$K%-!<%o!<%I$O$J$$(B |
$BFI$_9~$_@lMQJQ?t$NJDJq(B (closure) $B=q$-9~$_2DG=$JJQ?t$NJDJq(B (closure)
| (lambda (x) (f x y))
(lambda (x) (incf y x))
| lambda x: f(x, y)
$BIT2DG=!"%*%V%8%'%/%H$r;H$&$Y$7(B
|
$B0z?tEO$7(B |
Lisp $B$K$*$1$k0z?tEO$7(B |
Python $B$K$*$1$k0z?tEO$7(B |
$B>JN,2DG=$J0z?t(B
$B2DJQ8D$N0z?t(B
$BITFCDj$J%-!<%o!<%I$K$h$k0z?t(B
$B8F$S=P$7$N$-$^$j(B |
(defun f (&optional (arg val) ...)
(defun f (&rest arg) ...)
(defun f (&allow-other-keys &rest arg) ...)
$B%-!<%o!<%I$r;H$($k$N$O@k8@$5$l$?$H$-$N$_(B:
(defun f (&key x y) ...)
(f :y 1 :x 2) |
def f (arg=val): ...
def f (*arg): ...
def f (**arg): ...
$B$"$i$f$k4X?t$K%-!<%o!<%I$G0z?t$rEO$;$k(B:
def f (x,y): ...
f(y=1, x=2)
|
$B |
Lisp $B$K$*$1$k |
Python $B$K$*$1$k |
$B%3%s%Q%$%k(B
$B4X?t$N;2>H2r7h(B
$B@k8@(B |
$B%M%$%F%#%V%3!<%I$K%3%s%Q%$%k2DG=(B
$B$[$I$s$I$N4X?t(B/$B%a%=%C%I$N;2>H$,9bB.(B
$B8zN($r>e$2$k$?$a$N@k8@$,2DG=(B
|
$B%3%s%Q%$%k$O%P%$%H%3!<%I$N$_(B
$B$[$I$s$I$N4X?t(B/$B%a%=%C%I$N;2>H$OCY$$(B
$B@k8@$J$7(B
|
$B$=$NB>$N5!G=(B |
Lisp $B$K$*$1$k5!G=(B |
Python $B$K$*$1$k5!G=(B |
$B%/%)!<%H(B (quote) |
$B%j%9%H9=B$A4BN$r%/%)!<%H$9$k(B:
'hello
'(this is a test)
'(hello world (+ 2 2))
|
$B8D!9$NJ8;zNs$r%/%)!<%H$9$k(B$B!"$"$k$$$O(B .split():
'hello'
'this is a test'.split()
['hello', 'world', [2, "+", 2]]
|
$B<+J,<+?H$r@bL@$9$k5!G=(B |
(defun f (x)
"compute f value"
...)
> (documentation 'f 'function)
"compute f value"
|
def f(x):
"compute f value"
...
>>> f.__doc__
"compute f value"
|
$B%j%9%H$X$N%"%/%;%9(B |
$B4X?t7PM3$G%"%/%;%9$9$k(B:
(first list)
(setf (elt list n) val)
(first (last list))
(subseq list start end)
(subseq list start)
|
$B9=J8$G%"%/%;%9$9$k(B:
list[0]
list[n] = val
list[-1]
list[start:end]
list[start:]
|
$B%O%C%7%e%F!<%V%k$X$N%"%/%;%9(B |
$B4X?t7PM3$G%"%/%;%9$9$k(B:
(setq h (make-hash-table))
(setf (gethash "one" h) 1.0)
(gethash "one" h)
(let ((h (make-hash-table)))
(setf (gethash "one" h) 1)
(setf (gethash "two" h) 2)
h)
|
$B9=J8$G%"%/%;%9$9$k(B:
h = {}
h["one"] = 1.0
h["one"] $B$^$?$O(B h.get("one")
h = {"one": 1, "two": 2}
|
$B%j%9%H$NA`:n(B |
(cons x y)
(car x)
(cdr x)
(equal x y)
(eq x y)
nil
(length seq)
(vector 1 2 3)
| [x] + y $B$G$b$3$l$O$d$i$J$$$h$&$K(B
x[0]
x[1:] $B$G$b$3$l$O$d$i$J$$$h$&$K(B
x == y
x is y
None $B$^$?$O(B () $B$^$?$O(B [ ] $B$^$?$O(B 0
len(seq)
(1, 2, 3)
|
$BG[Ns$NA`:n(B |
(make-array 10 :initial-element 42)
(aref x i)
(incf (aref x i))
(setf (aref x i) 0)
(length x)
#(10 20 30) $B%5%$%:$,JQ$o$i$J$$>l9g(B
| 10 * [42]
x[i]
x[i] += 1
x[i] = 0
len(x)
[10, 20, 30]
|
$BB?$/$N?M!9$K=EMW$JItJ,$O(B Python $B$H(B Lisp $B$NB.EY$K4X$9$k$H$3$m$@$m$&!#(B
$B$"$J$?$N(B $BMxMQK!$KE,$7$?%Y%s%A%^!<%/$r$*$3$J$&$N$O$`$:$+$7$$$,!"(B
$B
5 $B$D$N8@8l$K$*$1$k(B 10$B8D$N%Y%s%A%^!<%/$NAjBPE*$JB.EYHf3S!#(B
The Great Computer Language Shootout $B$h$j!#(B
$B%F%9%H(B | Lisp | Java | Python | Perl | C++
|
---|
$B%O%C%7%e$X$N%"%/%;%9(B | 1.06 | 3.23 | 4.01 | 1.85 | 1.00
|
$BNc30=hM}(B | 0.01 | 0.90 | 1.54 | 1.73 | 1.00 | | $BK^Nc(B
|
---|
$B%U%!%$%kCf$N?tCM$rAm7W(B | 7.54 | 2.63 | 8.34 | 2.49 | 1.00 | | C++ $B$N(B 100$BG\0J>e(B
|
$B9T$rH?E>$5$;$k(B | 1.61 | 1.22 | 1.38 | 1.25 | 1.00 | | C++ $B$N(B 50$B!A(B100$BG\(B
|
$B9TNs$N>h;;(B | 3.30 | 8.90 | 278.00 | 226.00 | 1.00 | | C++ $B$N(B 10$B!A(B50$BG\(B
|
$B%R!<%W%=!<%H(B | 1.67 | 7.00 | 84.42 | 75.67 | 1.00 | | C++ $B$N(B 5$B!A(B10$BG\(B
|
$BG[Ns$X$N%"%/%;%9(B | 1.75 | 6.83 | 141.08 | 127.25 | 1.00 | | C++ $B$N(B 1$B!A(B5$BG\(B
|
$B%j%9%H=hM}(B | 0.93 | 20.47 | 20.33 | 11.27 | 1.00 | | C++ $B$N(B 0$B!A(B1$BG\(B
|
$B%*%V%8%'%/%H:n@.(B | 1.32 | 2.39 | 49.11 | 89.21 | 1.00
|
$BC18l$r?t$($k(B | 0.73 | 4.61 | 2.57 | 1.64 | 1.00
|
$BJ?6Q(B | 1.67 | 4.61 | 20.33 | 11.27 | 1.00 |
25% $B$+$i(B 75% | 0.93 $B$+$i(B 1.67 | 2.63 $B$+$i(B 7.00 | 2.57 $B$+$i(B 84.42 | 1.73 $B$+$i(B 89.21 | 1.00 $B$+$i(B 1.00 |
$BHO0O(B | 0.01 $B$+$i(B 7.54 | 0.90 $B$+$i(B 2.47 | 1.38 $B$+$i(B 278 | 1.25 $B$+$i(B 226 | 1.00 $B$+$i(B 1.00 |
$BB.EY$O(B C++ $BMQ$N(B g++ $B%3%s%Q%$%i$,(B 1.00 $B$K$J$k$h$&@55,2=$7$F$"$k!#(B
$B$@$+$i(B 2.00 $B$O$=$l$h$j(B 2$BG\CY$$$3$H$rI=$9!#(B0.01 $B$O(B 100$BG\B.$$$H$$$&$3$H$@!#(B
Lisp $B$O(B CMUCL $B%3%s%Q%$%i$r;H$C$F$$$k!#(B
$BGX7J$N?'$O1&$NK^Nc$K$7$?$,$C$FJQ$($F$"$k!#:G8e$N9T$O3F8@8l$+$i%H%C%W(B2 $B$H(B
$B%o!<%9%H(B2 $B$r=|$$$F(B 25% $B$+$i(B 75% $B$N$_$N7k2L$r=P$7$?$b$N$@!#(B
($B%H%C%W(B2 $B$H%o!<%9%H(B2 $B$r=|$$$F(B Lisp $B$H(B Python $B$rHf3S$7$?7k2L!"(B
Python $B$N$[$&$,(B Lisp $B$h$j$b(B 3$B!A(B85$BG\(B $BCY$$$3$H$,$o$+$C$?(B -- $B$3$l$O(B Perl $B$H$[$\F1$8$G!"(B
Java $B$d(B Lisp $B$h$j$:$C$HCY$$!#(BLisp $B$O(B Java $B$NLs(B 2$BG\$NB.$5$G$"$k!#(B)
Lisp $B%W%m%0%i%^$,(B Python $B$r3X$V$H$-$NMWE@(B
$B$3$3$K(B Lisp $B%W%m%0%i%^$G$"$k$o$?$7$,(B Python $B$r3X$s$@$H$-$K$R$C$+$+$C$?2U=j$r$^$H$a$F$_$?(B:
- $B%j%9%H$O(B Cons $B$G$O$J$$!#(B Python $B$N%j%9%H$OO(1) $B$G$9$`$,!"(Bcons $B$H(B cdr $B$r$D$+$C$?A`:n$KAjEv$9$k$b$N$O(B O(n) $B$N(B
$B?7$7$$5-21NN0h$r>CHq$7$F$7$^$&!#(Bcar/cdr $B$r;H$C$?:F5"$h$j$O(B map $B$d(B
for e in x: $B$r;H$C$?$[$&$,$$$$!#$"$H!"6u$N%j%9%H$,$?$@$R$H$D$G$O$J$/(B
$BJ#?t$G$"$k$3$H$K$OCm0U$7$F$*$/I,MW$,$"$k!#$3$l$O(B Lisp $B$G$h$/$"$k%P%0(B --
$B%f!<%6$,(B (nconc old new) $B$H$d$C$F(B old $B$,JQ99$5$l$k$H;W$$$3$_!"(B
$Bold $B$,(B nil $B$N$H$-$K$O2?$b5/$3$i$J$$(B -- $B$rKI$0!#(B
Python $B$G$O(B old.extend(new) $B$O$D$M$K$&$^$/F0:n$9$k!#$7$+$7$3$l$OF1;~$K!"(B
$B6u%j%9%H(B [] $B$KBP$7$F8!::$r9T$&$H$-$K$O$D$M$K(B == $B$r;H$o$J$1$l$P(B
$B$J$i$J$$$H$$$&$3$H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$G$"$k!#(B
$B$3$l$,0UL#$9$k$H$3$m$O!"(B Python $B$O(B Scheme $B$N$h$&$K(B
$B$R$H$D$NL>A06u4V$r4X?t$HJQ?t$K;H$C$F$$$k$H$$$&$3$H$@!#(B
Common Lisp $B$N$h$&$KJL!9$K$O$7$F$$$J$$!#$?$H$($P(B:
def f(list, len): return list((len, len(list))) ## $B$^$:$$(B Python
(define (f list length) (list length (length list))) ;; $B$^$:$$(B Scheme
(defun f (list length) (list length (length list))) ;; $B@5$7$$(B Common Lisp
$B$3$N$3$H$O%U%#!<%k%I$H%a%=%C%I$K$b$"$F$O$^$k!#(B
$B%a%=%C%I$HF1$8L>A0$N%U%#!<%k%I$r;H$C$F%G!<%?Cj>]$r$*$3$J$&$3$H$O$G$-$J$$(B:
class C:
def f(self): return self.f ## $B$^$:$$(B Python
...
- Python 2.1 $B0JA0$O;z6gE*%9%3!<%W(B (lexical scope) $B$r%5%]!<%H$7$F$$$J$$!#(B
$B%P!<%8%g%s(B 2.1 $B0JA0$N(B Python $B$G$O!"3F%b%8%e!<%k$K$O(B 2$B$D$N(B
$BJQ?t%9%3!<%W$7$+$J$+$C$?!#%0%m!<%P%k$J%9%3!<%W$H!"4X?t$K$*$1$k%9%3!<%W$G$"$k!#(B
2001$BG/(B 4$B7n$K%j%j!<%9$5$l$?(B Python 2.1 $B$G$O!"(B
"from __future__ import nested_scopes" $B$r$`$H$3$m$@$m$&!#(B
$B$3$l$G!"30B&$NJQ?t$r;2>H$9$kJDJq$r$D$/$k$3$H$,$G$-$k!#30B&$NJQ?t$NFbMF$r(B
$BJQ99$7$?$$>l9g!"A*Br;h$O$9$3$7$7$+$J$/!"$I$l$b>/!9def sum(items):
total = [0.0]
def f(x): total[0] = total[0] + x
map(f, items)
return total[0]
>>> sum([1.1, 2.2, 3.3])
6.6
$B$3$3$G$O(B lambda $B$r;H$&$3$H$O$G$-$J$$$H$$$&$3$H$KCm0U$7$F$[$7$$!#(B
$B$J$<$J$i(B lambda $B4X?t$NK\BNItJ,$OC10l$N<0$G$J$1$l$P$J$i$:!"%9%F!<%H%a%s%H$O(B
$B;H$($J$$$+$i$@!#$3$l0J30$NA*Br;h$O4X?t$N$+$o$j$K%*%V%8%'%/%H$r;H$&$3$H$@$,!"(B
$B$3$l$O$$$5$5$+>iD9$9$.$k!#$7$+$7>iD9$5$H$$$&$N$O$=$l$r8+$k(lambda (x) (* k x)) $B$,$^$C$H$&$J$d$jJ}$@$H;W$&$@$m$&$,!"(B
Smalltalk $B$N%W%m%0%i%^$O$3$l$G$bB?$9$.$k$H9M$($k!#H`$i$O(B [:x | x * k] $B$r(B
$B;H$&$N$@!#$3$l$K$R$-$+$((B Java $B%W%m%0%i%^$O!"iD9$J(B inner class $BI=5-$r;}$A$@$7$F$/$k$@$m$&!#(B
new Callable() {
public Object call(Object x) {
return x.times(k)
}
}
- Python $B$NJ8;zNs$O(B Lisp $B$N%7%s%\%k$H$OA4A3$A$,$&!#(B
Python $B$O!"%b%8%e!<%k$d%/%i%9Cf$KB8:_$9$k%O%C%7%e%F!<%V%k$K(B
$BJ8;zNs$r(B intern $B$5$;$k$3$H$G%7%s%\%k$N8!:w$r$*$3$J$C$F$$$k!#(B
$B$D$^$j(B obj.slot $B$H=q$/$H!"(BPython $B$Oobj $B$N%O%C%7%e%F!<%V%k$GJ8;zNs(B "slot" $B$r(B
$B8!:w$9$k!#$^$?!"(BPython $B$O%f!<%6$N%3!<%ICf$K$"$k(B x = "str" $B$J$I$N(B
$B$h$&$J$$$/$D$+$NJ8;zNs$b(B intern $B$9$k!#$@$,JQ?t$i$7$/8+$($J$$J8;zNs$O(B
intern $B$5$l$J$$!#$?$H$($P(B x = "a str" $B$O(B intern $B$5$l$J$$(B
($B$3$N$3$H$r;XE&$7$F$/$l$?(B Brian Spilsbury $B$K46 Python $B$K$O%^%/%m$,$J$$!#(B
Python $B$O%W%m%0%i%`$NCj>]E*$J9=J8LZ$K%"%/%;%9$9$k5!G=$r(B
$B$b$C$F$$$k$,!"$3$l$O$J$K$b$S$C$/$j$9$k$h$&$J$3$H$G$O$J$$!#(B
$B$3$N%b%8%e!<%k$ND9=j$OM}2r$9$k$N$,4JC1$@$H$$$&$3$H$G!"(B
5$B9T$[$I$N%3!<%I$r;H$C$F!"(B5$BJ,$+$=$3$i$G>>> parse("2 + 2")
['eval_input', ['testlist', ['test', ['and_test', ['not_test', ['comparison',
['expr', ['xor_expr', ['and_expr', ['shift_expr', ['arith_expr', ['term',
['factor', ['power', ['atom', [2, '2']]]]], [14, '+'], ['term', ['factor',
['power', ['atom', [2, '2']]]]]]]]]]]]]]], [4, ''], [0, '']]
$B$3$N7k2L$K$O$+$J$j$,$C$+$j$7$?!#(BLisp $B$J$i!"$3$l$HF1Ey$N9=J8LZ$O(B
(+ 2 2) $B$G$"$k!#$3$N9=J8LZ$J$iC/$K$G$b;H$($k$,!"(B
Python $B$N$3$s$J9=J8LZ$r$$$8$l$k$N$OK\Ev$N%(%-%9%Q!<%H$@$1$@$m$&!#(B
$BJ8;zNs$rO"7k$7$F%^%/%m$K;w$?$h$&$J$b$N$O:n$l$k$+$b$7$l$J$$!#(B
$B$=$l$G$b$=$N5!G=$O8@8l$N$=$NB>$NItJ,$HE}9g$5$l$F$$$J$$$N$G!"(B
$B$B%G!<%?(B $B$r(B Python $B$G(B
$B$D$/$k$3$H$K$h$C$F
Lisp $B%W%m%0%i%`$H(B Python $B%W%m%0%i%`$NHf3S(B
$B:G=i$N%5%s%W%k%W%m%0%i%`$O(B
Paradigms of Artificial Intelligence Programming
$B$+$i$H$C$F$-$?!"(B$B4JC1$J%i%s%@%`J8@8@.4o(B
$B$r(B Python $B$KK]Lu$7$?$b$N$@!#7kO@(B: $B4J7i$5$H$$$&E@$G$O;w$?$h$&$J$b$N$G$"$k!#(B
grammar[phrase] $B$,(B (rule-rhs (assoc phrase *grammar*)) $B$h$j(B
$B4JC1$@$H$$$&E@$G$O(B Python $B$N$[$&$,$^$5$C$F$$$k$,!"(B['NP', 'VP'] $B$r(B
'(NP VP) $B$H=q$1$kE@$G$O(B Lisp $B$N$[$&$,$^$5$C$F$$$k!#(B
Python $B$N%W%m%0%i%`$N$[$&$,8zN($,0-$=$&$@$,!"4N?4$JE@$O$=$3$G$O$J$$!#(B
$B$I$A$i$N8@8l$b$3$N$B$A$c$s$H8+$k$?$a$K!"%V%i%&%6$N%&%$%s%I%&$r9-$2$F$[$7$$!#(B
Lisp $B%W%m%0%i%`(B simple.lisp
|
Python $B%W%m%0%i%`(B simple.py
|
(defparameter *grammar*
'((sentence -> (noun-phrase verb-phrase))
(noun-phrase -> (Article Noun))
(verb-phrase -> (Verb noun-phrase))
(Article -> the a)
(Noun -> man ball woman table)
(Verb -> hit took saw liked))
"$B$h$/$"$k1Q8l$NJ8K!$N%5%V%;%C%H!#(B")
(defun generate (phrase)
"$B%i%s%@%`$JJ8$"$k$$$O6g$r@8@.$9$k!#(B"
(cond ((listp phrase)
(mappend #'generate phrase))
((rewrites phrase)
(generate (random-elt (rewrites phrase))))
(t (list phrase))))
(defun generate-tree (phrase)
"$B%i%s%@%`$JJ8$"$k$$$O6g$r!"40A4$J9=J8LZ$D$-$G@8@.$9$k!#(B"
(cond ((listp phrase)
(mapcar #'generate-tree phrase))
((rewrites phrase)
(cons phrase
(generate-tree (random-elt (rewrites phrase)))))
(t (list phrase))))
(defun mappend (fn list)
"$B%j%9%H$N3FMWAG$K4X?t(B fn $B$rE,MQ$5$;$?7k2L$r(B append $B$9$k!#(B
mapcon $B$N$h$&$J$b$N$@$,!"(Bnconc $B$N$+$o$j$K(B append $B$r;H$C$F$$$k!#(B"
(apply #'append (mapcar fn list)))
(defun rule-rhs (rule)
"$B@8@.5,B'$N1&JU!#(B"
(rest (rest rule)))
(defun rewrites (category)
"$B$3$N%+%F%4%j$KBP$9$k2DG=$J=q$-49$($N%j%9%H$rJV$9!#(B"
(rule-rhs (assoc category *grammar*)))
|
from whrandom import choice
def Dict(**args): return args
grammar = Dict(
S = [['NP','VP']],
NP = [['Art', 'N']],
VP = [['V', 'NP']],
Art = ['the', 'a'],
N = ['man', 'ball', 'woman', 'table'],
V = ['hit', 'took', 'saw', 'liked']
)
def generate(phrase):
"$B%i%s%@%`$JJ8$"$k$$$O6g$r@8@.$9$k!#(B"
if is_list(phrase):
return mappend(generate, phrase)
elif phrase in grammar:
return generate(choice(grammar[phrase]))
else: return [phrase]
def generate_tree(phrase):
"$B%i%s%@%`$JJ8$"$k$$$O6g$r!"40A4$J9=J8LZ$D$-$G@8@.$9$k!#(B"
if is_list(phrase):
return map(generate_tree, phrase)
elif phrase in grammar:
return [phrase] + generate_tree(choice(grammar[phrase]))
else: return [phrase]
def mappend(fn, list):
"$B%j%9%H$N3FMWAG$K4X?t(B fn $B$rE,MQ$5$;$?7k2L$r(B append $B$9$k!#(B"
return reduce(lambda x,y: x+y, map(fn, list))
def is_list(x): return type(x) is type([])
|
$B$3$N(B Lisp $B%W%m%0%i%`$rAv$i$;$k(B
|
$B$3$N(B Python $B%W%m%0%i%`$rAv$i$;$k(B
|
> (generate 'S)
(the man saw the table)
|
>>> generate('S')
['the', 'man', 'saw', 'the', 'table']
>>> string.join(generate('S'))
'the man saw the table'
|
Python $B$N%3!<%I$K4^$^$l$F$$$kJ8K!$,(B Lisp $B$N$=$l$h$j1x$J$/$J$C$F$$$k$N$,(B
$B$$$d$@$C$?$N$G!"(BPython $B$G9=J82r@O4o$r=q$/(B ($B$"$H$G$3$l$K$O%U%j!<$G(B
$B;HMQ2DG=$J$b$N$,$9$G$K$$$/$D$+$"$k$3$H$rCN$C$?(B) $B$+!"AH$_9~$_1i;;;R$r(B
$B%*!<%P!<%m!<%I$9$k$3$H$r9M$($?!#8eExpr $B%/%i%9(B
$B$J$I$Gl$+$.$j$N%"%I%[%C%/$J@8@.5,B'%Q!<%5$G$$$$$@$m$&!#$D$^$j(B
$B@8@.5,B'$O(B '|' $B$G6h@Z$i$l$?8uJd$N%j%9%H$+$i$J$j!"3F8uJd$O(B
' ' $B$G6h@Z$i$l$?C18l$N%j%9%H$K$J$C$F$$$k!"$H$$$&$h$&$K!#$=$7$F(B
Lisp $B$G$G$-$?%W%m%0%i%`$rK]Lu$9$k$h$j$b(B Python $B$N47=,$K$7$?$,$C$?(B
$BJ8K!%W%m%0%i%`$K$7$?$i!"
Python $B%W%m%0%i%`(B simple.py ($B47=,%P!<%8%g%s(B)
|
"""$BM?$($i$l$?J8K!$+$i%i%s%@%`$JJ8$r@8@.$9$k%b%8%e!<%k!#J8K!$O(B
S = 'NP VP | S and S' $B$N$h$&$J7A<0$N%(%s%H%j$G9=@.$5$l$k!#3F%(%s%H%j$O(B
{'S': [['NP', 'VP'], ['S', 'and', 'S']]} $B$N$h$&$J7A<0$KJQ49$5$l!"(B
$B$3$l$O$=$l$>$l$N30B&$NBg$-$J%j%9%H(B (top-level lists) $B$+$i$I$l$+(B
$B$R$H$D$N=q$-49$(8uJd$,%i%s%@%`$KA*$P$l$k$3$H$r<($9!#$=$7$F!"A*$P$l$?(B
$B%j%9%H$NFbB&$K$"$k3FMWAG(B (each element of the second-level list) $B$,(B
$B=q$-49$($i$l$k!#$=$NMWAG$,J8K!Cf$K$J$1$l$P!"3FMWAG$O$=$l<+?H$K=q$-(B
$B49$($i$l$k!#4X?t(B rewrite $B$*$h$S(B rewrite_tree $B$O$=$l$>$lC18l$N%j%9%H$H!"(B
$B7k2L$,DI2C$5$l$k%"%-%e%`%l!<%?(B ($B6u%j%9%H(B) $B$r0z?t$H$7$F$H$k$h$&$K$J$C$F$$$k!#(B
$B4X?t(B generate $B$*$h$S(B generate_tree $B$O!"(Brewrite $B$H(B rewrite_tree $B$X$N(B
$B4JJX$J%$%s%?%U%'%$%9$rDs6!$7$F$*$j!"$3$l$OJ8;zNs(B ($B%G%U%)%k%H$O(B 'S') $B$r(B
$BF~NO$H$7$F |
Peter Norvig
Japanese Translation Changes:
$B8x3+!#(B(2002/10/13)
$B?
Yusuke Shinyama <yusuke at cs . nyu . edu>