読者です 読者をやめる 読者になる 読者になる

by shigemk2

当面は技術的なことしか書かない

対話によるCommon Lisp入門 40 assoc

読書ノート

連想リストのうち、キーが等しい対があれば、そのうち最も左のものを返す。

[1]> (assoc 'two '((one . 1) (two . 2) (three . 3)))
(TWO . 2)
[2]> (assoc 'four '((one . 1) (two . 2) (three . 3)))
NIL
[3]> (assoc 'one '((one . 1) (two . 2) (three . 3)))
(ONE . 1)
[4]> (assoc 1 '((one . 1) (two . 2) (three . 3)))
NIL
[5]> (defun assoc$ (item alist)
(if (null alist) nil
(if (eql item (first (first alist)))
(first alist)
(assoc$ item (rest alist)))))
ASSOC$
[6]> (assoc$ one '((one . 1) (two . 2) (three . 3)))

*** - SYSTEM::READ-EVAL-PRINT: variable ONE has no value
The following restarts are available:
USE-VALUE      :R1      Input a value to be used instead of ONE.
STORE-VALUE    :R2      Input a new value for ONE.
ABORT          :R3      Abort main loop
Break 1 [7]> :a
[8]> (assoc$ 'one '((one . 1) (two . 2) (three . 3)))
(ONE . 1)