2014-11-03 67 views
1

我有以下幾點,我正在使用maphash遍歷hashmap。處理hashmap中每個元素的lambda函數接收兩個參數,一個鍵和一個值。但我從不使用這個值,所以在編譯時我收到一個警告。我如何解決這個警告?在maphash lambda函數中忽略參數

回答

2
? (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 
2

賴的已經指出(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)