連想配列
連想配列にはassocを使う。
gosh> (assoc "b" '(("a" 1) ("b" 2))) ("b" 2) gosh> (assoc "c" '(("a" 1) ("b" 2))) #f gosh> (cadr (or (assoc "b" '(("a" 1) ("b" 2))) '("" ""))) 2 gosh> (cadr (or (assoc "c" '(("a" 1) ("b" 2))) '("" ""))) ""
cadrでエラーが出てしまうのを or で防いでいるのがいまいち。
gosh> (let ((x (assoc "a" '(("a" 1) ("b" 2))))) (if x (cadr x) "")) 1 gosh> (let ((x (assoc "c" '(("a" 1) ("b" 2))))) (if x (cadr x) "")) ""
こうかな。
取りあえず関数化していみないw
gosh> ((lambda (x) (let ((x (assoc x '(("a" 1) ("b" 2))))) (if x (cadr x) ""))) "a") 1 gosh> ((lambda (x) (let ((x (assoc x '(("a" 1) ("b" 2))))) (if x (cadr x) ""))) "b") 2 gosh> ((lambda (x) (let ((x (assoc x '(("a" 1) ("b" 2))))) (if x (cadr x) ""))) "c") ""