在maphash lambda函数中忽略参数
问题描述:
我有以下几点,我正在使用maphash遍历hashmap。处理hashmap中每个元素的lambda函数接收两个参数,一个键和一个值。但我从不使用这个值,所以在编译时我收到一个警告。我如何解决这个警告?在maphash lambda函数中忽略参数
答
? (defun foo (a b) (+ a 2))
;Compiler warnings :
; In FOO: Unused lexical variable B
FOO
? (defun foo (a b)
(declare (ignore b))
(+ a 2))
FOO
答
赖的已经指出(declare (ignore ...))
(这是已经存在于另一个问题,实际上)。如果您有兴趣使用另一种方式来遍历散列表键值(或值),则可以使用loop:
(let ((table (make-hash-table)))
(dotimes (x 5)
(setf (gethash x table) (format nil "~R" x)))
(values
(loop for value being each hash-value of table
collect value)
(loop for key being each hash-key of table
collect key)))
;=>
; ("zero" "one" "two" "three" "four")
; (0 1 2 3 4)