小编JEP*_*ice的帖子

Lisp:使用语法糖访问递归哈希

我正在尝试构建一个函数(或宏)来简化哈希表中数据的获取和设置(意思是哈希内的哈希,哈希内的哈希等)。我不认为我可以使用宏来实现,而且我不确定如何使用eval来实现。我希望能够执行以下操作:

(gethashdeep *HEROES* "Avengers" "Retired" "Tony Stark")
Run Code Online (Sandbox Code Playgroud)

并得到回报“钢铁侠”

散列都是通过以下方式创建的:

(setf hashtablename (make-hash-table :test 'equal))
Run Code Online (Sandbox Code Playgroud)

然后从那里开始

我可以执行以下操作,但是想要对其进行抽象,以便可以从任意深度以编程方式提取值:

;;pulling from a hash that's 2 deep
(gethash "Tony Stark" (gethash "Avengers" *HEROES*))
Run Code Online (Sandbox Code Playgroud)

更新-我的努力:

(defun getdeephash (hashpath h k)
  (let* ((rhashpath (reverse hashpath))
    (hashdepth (list-length hashpath))
    (hashcommand (concatenate 'string "(gethash \"" k "\"")))
   (loop for i from 1 to hashdepth
      do (setf hashcommand (concatenate 'string hashcommand "(gethash \"" (nth (- i 1) rhashpath) "\"")))
      (setf hashcommand (concatenate 'string  hashcommand " "  h (make-string (- …
Run Code Online (Sandbox Code Playgroud)

lisp common-lisp

2
推荐指数
2
解决办法
334
查看次数

标签 统计

common-lisp ×1

lisp ×1