際際滷

際際滷Share a Scribd company logo
18. Destructurning
Destructuring is a generalization
of assignment.
Destructuring :朱 .
 18.1 Destructuring on Lists
 destructuring 螳
 list襯 destructruring 覯.
 18.2 Other Structures
 dbind(destructuing-bind) 蟲  る
 覓語, 覯″, 覦一, 蟲譟一牡襯 destructuring 覯.
 18.3 Reference
 語ろ伎るゼ destructuring 覯.
 18.4 Matching
 企Г豺 伎
 if-match蟲(1, 2) 覦   る.
18.1 Destructuring on Lists
 (assign)
 seq, setf
 蠏(access) & (assign)
 destructuring-bind
Destructuring-bind
 CLTL2, Common Lisp
destructurning-bind 襦 襷ろ襦襯 豢
螳.
Ex) destrucing-bind
 貊螳 讌ъ讌 訖襷, 覲企 覈伎
.
CLTL1
(let ((x (first lst))
(y (second lst))
(z (third lst)))
(list x y z))
CLTL2 れ伎
(destructuring-bind (x y z) lst
(list x y z))
Destructuring
 Destructuring CLTL1 Common Lisp
 譟伎.
 讌蠍 磯 覿襴螳 讌襷, 
destructuring-bind 襷ろ襦 襷り覲 襴
碁ゼ 覿襴る  貊企.
18.2 Other Structures
 襴ろ碁ゼ destructuring一  蟇
螳 .
 蠏碁讌襷, 覲旧″ 螳豌伎  伎 
.
 > (dbind (a (b . c) &rest d) '(1 "fribble" 2 3 4)
 (list a b c d))
 (1 #f "ribble" (2 3 4))
覓語願骸 覦燕
 read-macro # - 覓語襯 蠍  
 CL-USER> (coerce "123" 'list)
 (#1 #2 #3)
 read-macro #( - 覯″磯ゼ 蠍  
 CL-USER> (vector 1 2 3)
 #(1 2 3)
 CL-USER> (make-array 3 :initial-contents '(1 2 3))
 #(1 2 3)
 CL-USER> (make-array '(2 3) :initial-contents '((1 2 3) (4 5
6)))
 #2A((1 2 3) (4 5 6))
 *print-array*
Dbind
 讌蠍蟾讌 覺 襷ろ襦り骸 觜蟲覃,
dbind 蠏覈螳  ク伎襷,
 Lisp 襦蠏碁覦 蟯 general lesson
願蠍一,  企至 讌 
危 蠏語蟆 ,  襷ろ襦 蟲
  螻給 螳豺螳 .
Ex) Dbind
 (defmacro dbind (pat seq &body body)
(let ((gseq (gensym)))
`(let ((,gseq ,seq))
,(dbind-ex (destruc pat gseq #'atom) body))))
 dbind destructuring-bind豌 壱
譯殊伎 り  襯 詞 覈
る, 襯 覦
Ex) with-
 with-matrix
 覦一伎  蠍磯
 with-array
 覦一伎 譬 蠍磯
 with-struct
 蟲譟一牡 prefix  name 蠍磯
18.3 Reference
 let symbol-macrolet朱 ル蟆 襷
朱, call-by-name 覯
destructuring 襷ろ襦襯 襷  .
ex) with-places
 (defmacro with-places (pat seq &body body)
(let ((gseq (gensym)))
`(let ((,gseq ,seq))
,(wplac-ex (destruc pat gseq #'atom) body))))
 Wplac-ex 企  dbind-ex 覃, let
symbol-macrolet朱 覦.
18.4 Matching
 destructuring 朱 企朱,
企Г豺 朱 destructuring企.
 ``企Г豺''企 企  覩碁ゼ 讌
螻 讌襷, 蠍一, 覲襯 螻 
 襭蟲譟磯ゼ 觜蟲, 螳螳 狩覃
覲 螳  蟆 覩誤蠍磯 
.
狩(Unification)
 企Г豺螻 狩 るゴ.
 企Г豺 覦, 狩 覦
 X = Y
X = 5
 企Г豺 豌覯讌 殊碁 (Y螳 bind讌 蠍一) 
Erlang
 狩 Y 5 螳 螳蟆.  Prolog
 http://stackoverflow.com/questions/4442314/differences-
between-pattern-matching-and-unification
 Unification pattern matching る危蟆.
Match
 語襦 覦蟆 朱Μ 觜蟲 覲
 螳    襦 襷れ 譴
(bindings).
 (match '(p ?x b ?y a) '(p ?y b c a))
 ((?Y . C) (?X . ?Y)) ;; 襷れ cons襦 覓苦伎
 T ;; 襷れ讌 れ朱 flag
Ex)match
(defun match (x y &optional binds)
(acond2
((or (eql x y) (eql x '_) (eql y '_)) (values binds t))
((binding x binds) (match it y binds))
((binding y binds) (match x it binds))
((varsym? x) (values (cons (cons x y) binds) t))
((varsym? y) (values (cons (cons y x) binds) t))
((and (consp x) (consp y) (match (car x) (car y) binds))
(match (cdr x) (cdr y) it))
(t (values nil nil))))
if-match ( 1 )
 (defun abab (seq)
(if-match (?x ?y ?x ?y) seq
(values ?x ?y)
nil))
  match binding 伎 if-match
襯 蟲.
Ex) if-match ( 1 )
(defmacro if-match (pat seq then &optional else)
`(aif2 (match ',pat ,seq)
(let ,(mapcar
#'(lambda (v) `(,v (binding ',v it)))
(vars-in then #'atom))
,then)
,else))
If-match ( 2 ).1
  if-match(1) 讌ъ襷, 壱 覓 襷
殊 (觜)
 企 螳 if-match(2) 蠍語襷, 貉危殊
襷 殊 .
 If-match 豌覯讌 語襦 れ伎る 蟆 朱
覲螳  企襦 る, 貉危
 Match 蟯覲襯    蟆企
.
If-match ( 2 ).2
 企 Quote蟆 , 螳
.
 朱覲 quote  企襦
伎  .
Ex) if-match ( 2 )
 (defmacro if-match2 (pat seq then &optional else)
`(let ,(mapcar #'(lambda (v) `(,v ',(gensym)))
(vars-in pat #'simple?))
(pat-match ,pat ,seq ,then ,else)))
 (defmacro pat-match (pat seq then else)
(if (simple? pat)
(match1 .. ---- A
(with-gensym  ---- B
 A  れ 螻
 B - gensym  企 bind讌 蠍一, binding
 貊襯 燕
Ex) match1
 (defun match1 (refs then else)
(dbind ((pat expr) . rest) refs
(cond ((gensym? pat) . -- A
((var? pat) .. -- B
 A : length-test
 B : binding
394p - 244
 ?x襯 螻 る?
 (if-match (?x . ?rest1) lst1
(if-match (?x . ?rest2) lst2
?x))
 豢  19 with-answer螻, 24レ
with-inference襦 願屋
襷豺覃

 螳? Destructuring , 覃
讌 match蟾讌 危エ覺.
 襾語 レ 企 豌 蠍磯 
襦蠏碁  る.

More Related Content

On lisp ch18

  • 1. 18. Destructurning Destructuring is a generalization of assignment.
  • 2. Destructuring :朱 . 18.1 Destructuring on Lists destructuring 螳 list襯 destructruring 覯. 18.2 Other Structures dbind(destructuing-bind) 蟲 る 覓語, 覯″, 覦一, 蟲譟一牡襯 destructuring 覯. 18.3 Reference 語ろ伎るゼ destructuring 覯. 18.4 Matching 企Г豺 伎 if-match蟲(1, 2) 覦 る.
  • 3. 18.1 Destructuring on Lists (assign) seq, setf 蠏(access) & (assign) destructuring-bind
  • 4. Destructuring-bind CLTL2, Common Lisp destructurning-bind 襦 襷ろ襦襯 豢 螳.
  • 5. Ex) destrucing-bind 貊螳 讌ъ讌 訖襷, 覲企 覈伎 . CLTL1 (let ((x (first lst)) (y (second lst)) (z (third lst))) (list x y z)) CLTL2 れ伎 (destructuring-bind (x y z) lst (list x y z))
  • 6. Destructuring Destructuring CLTL1 Common Lisp 譟伎. 讌蠍 磯 覿襴螳 讌襷, destructuring-bind 襷ろ襦 襷り覲 襴 碁ゼ 覿襴る 貊企.
  • 7. 18.2 Other Structures 襴ろ碁ゼ destructuring一 蟇 螳 . 蠏碁讌襷, 覲旧″ 螳豌伎 伎 . > (dbind (a (b . c) &rest d) '(1 "fribble" 2 3 4) (list a b c d)) (1 #f "ribble" (2 3 4))
  • 8. 覓語願骸 覦燕 read-macro # - 覓語襯 蠍 CL-USER> (coerce "123" 'list) (#1 #2 #3) read-macro #( - 覯″磯ゼ 蠍 CL-USER> (vector 1 2 3) #(1 2 3) CL-USER> (make-array 3 :initial-contents '(1 2 3)) #(1 2 3) CL-USER> (make-array '(2 3) :initial-contents '((1 2 3) (4 5 6))) #2A((1 2 3) (4 5 6)) *print-array*
  • 9. Dbind 讌蠍蟾讌 覺 襷ろ襦り骸 觜蟲覃, dbind 蠏覈螳 ク伎襷, Lisp 襦蠏碁覦 蟯 general lesson 願蠍一, 企至 讌 危 蠏語蟆 , 襷ろ襦 蟲 螻給 螳豺螳 .
  • 10. Ex) Dbind (defmacro dbind (pat seq &body body) (let ((gseq (gensym))) `(let ((,gseq ,seq)) ,(dbind-ex (destruc pat gseq #'atom) body)))) dbind destructuring-bind豌 壱 譯殊伎 り 襯 詞 覈 る, 襯 覦
  • 11. Ex) with- with-matrix 覦一伎 蠍磯 with-array 覦一伎 譬 蠍磯 with-struct 蟲譟一牡 prefix name 蠍磯
  • 12. 18.3 Reference let symbol-macrolet朱 ル蟆 襷 朱, call-by-name 覯 destructuring 襷ろ襦襯 襷 .
  • 13. ex) with-places (defmacro with-places (pat seq &body body) (let ((gseq (gensym))) `(let ((,gseq ,seq)) ,(wplac-ex (destruc pat gseq #'atom) body)))) Wplac-ex 企 dbind-ex 覃, let symbol-macrolet朱 覦.
  • 14. 18.4 Matching destructuring 朱 企朱, 企Г豺 朱 destructuring企. ``企Г豺''企 企 覩碁ゼ 讌 螻 讌襷, 蠍一, 覲襯 螻 襭蟲譟磯ゼ 觜蟲, 螳螳 狩覃 覲 螳 蟆 覩誤蠍磯 .
  • 15. 狩(Unification) 企Г豺螻 狩 るゴ. 企Г豺 覦, 狩 覦 X = Y X = 5 企Г豺 豌覯讌 殊碁 (Y螳 bind讌 蠍一) Erlang 狩 Y 5 螳 螳蟆. Prolog http://stackoverflow.com/questions/4442314/differences- between-pattern-matching-and-unification Unification pattern matching る危蟆.
  • 16. Match 語襦 覦蟆 朱Μ 觜蟲 覲 螳 襦 襷れ 譴 (bindings). (match '(p ?x b ?y a) '(p ?y b c a)) ((?Y . C) (?X . ?Y)) ;; 襷れ cons襦 覓苦伎 T ;; 襷れ讌 れ朱 flag
  • 17. Ex)match (defun match (x y &optional binds) (acond2 ((or (eql x y) (eql x '_) (eql y '_)) (values binds t)) ((binding x binds) (match it y binds)) ((binding y binds) (match x it binds)) ((varsym? x) (values (cons (cons x y) binds) t)) ((varsym? y) (values (cons (cons y x) binds) t)) ((and (consp x) (consp y) (match (car x) (car y) binds)) (match (cdr x) (cdr y) it)) (t (values nil nil))))
  • 18. if-match ( 1 ) (defun abab (seq) (if-match (?x ?y ?x ?y) seq (values ?x ?y) nil)) match binding 伎 if-match 襯 蟲.
  • 19. Ex) if-match ( 1 ) (defmacro if-match (pat seq then &optional else) `(aif2 (match ',pat ,seq) (let ,(mapcar #'(lambda (v) `(,v (binding ',v it))) (vars-in then #'atom)) ,then) ,else))
  • 20. If-match ( 2 ).1 if-match(1) 讌ъ襷, 壱 覓 襷 殊 (觜) 企 螳 if-match(2) 蠍語襷, 貉危殊 襷 殊 . If-match 豌覯讌 語襦 れ伎る 蟆 朱 覲螳 企襦 る, 貉危 Match 蟯覲襯 蟆企 .
  • 21. If-match ( 2 ).2 企 Quote蟆 , 螳 . 朱覲 quote 企襦 伎 .
  • 22. Ex) if-match ( 2 ) (defmacro if-match2 (pat seq then &optional else) `(let ,(mapcar #'(lambda (v) `(,v ',(gensym))) (vars-in pat #'simple?)) (pat-match ,pat ,seq ,then ,else))) (defmacro pat-match (pat seq then else) (if (simple? pat) (match1 .. ---- A (with-gensym ---- B A れ 螻 B - gensym 企 bind讌 蠍一, binding 貊襯 燕
  • 23. Ex) match1 (defun match1 (refs then else) (dbind ((pat expr) . rest) refs (cond ((gensym? pat) . -- A ((var? pat) .. -- B A : length-test B : binding
  • 24. 394p - 244 ?x襯 螻 る? (if-match (?x . ?rest1) lst1 (if-match (?x . ?rest2) lst2 ?x)) 豢 19 with-answer螻, 24レ with-inference襦 願屋
  • 25. 襷豺覃 螳? Destructuring , 覃 讌 match蟾讌 危エ覺. 襾語 レ 企 豌 蠍磯 襦蠏碁 る.