Shammer's Philosophy

My private adversaria

Lisp 独自 dictionary version 20111029

Lisp 独自 dictionary version 20111025 - Shammerismの続き。先の実装では、同一キーを持つものがいくつも登録されてしまう実装だったので、同じキーを持つものが登録された場合は既存のものを上書きするように変更。以下のような感じになった。

(defun add-keypair-to-dictionary (source pair)
  (append source (list pair)))

(defun add-keyvalue-to-dictionary (source key value)
  (if (null source)
      (list (cons key value))
      (let ((new-dictionary (list (cons key value))))
	(dolist (x source)
	  (unless (string-equal key (car x))
	    (setf new-dictionary (add-keypair-to-dictionary new-dictionary x))))
	new-dictionary)))

(defun show-dictionary (dictionary)
  (format t "==========~%")
  (dolist (x dictionary)
    (format t "~A~%" x))
  (format t "==========~%"))

(defun get-keypair-from-dictionary (target key)
  (dolist (x target)
    (when (string-equal key (car x))
      (format t "~A~%" (cdr x)))))

(defun get-keyvalue-from-dictionary (target key)
  (cdr (get-keypair-from-dictionary target key)))

(defun remove-keypair-from-dictionary (target key)
  (let ((new-dictionary nil))
    (dolist (x target)
      (unless (string-equal key (car x))
	(setf new-dictionary (add-keypair-to-dictionary new-dictionary x))))
    new-dictionary))

add-keyvalue-to-dictionary 以外はほとんど同じ。いや、全く変わっていないかも。これで、追加、変更、削除をできるようになった。。。はず。簡易Hashtableとしては十分だろう、と思われる。