LoginSignup
1
1

More than 5 years have passed since last update.

リストからハッシュ表を作成

Last updated at Posted at 2014-10-13

はじめに

最近Common Lispの勉強をやっています。
思いついたもので簡単なものでも、とりあえず自分で書いてみようと思いました。
標準で用意されているかもしれませんが、今回はリストからハッシュ表を作成する関数を書いてみました。

ソースコード

create_hash_table.lisp
(defun list->hash (list)
  (let ((hash-table (make-hash-table)))
    (labels ((create-hash-table (list)
               (if (null list)
                   nil
                   (progn
                     (setf (gethash (first list) hash-table) (second list))
                     (create-hash-table (cddr list))))))
      (create-hash-table list))
    hash-table))
;=> LIST->HASH

(defparameter *hash-table* (list->hash '(a 1 b 2)))
;=> *HASH-TABLE*
(gethash 'a *hash-table*)
;=> 1, T
(gethash 'b *hash-table*)
;=> 2, T

おわりに

まだ基本的なことしか理解出来ていないのですが、少しずつ上達していきたいと思っています。

追記

公開した後に気に入らないところが色々あったので何度も更新しています。

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1