70 exercises in 20 chapters, from first s-expressions to restarts, macros, CLOS, and a tiny Lisp interpreter. Each exercise ships broken with a solved twin and a hint. Includes a watch-mode runner, a plain-CL check harness, a nix flake, and verification tooling. All plain SBCL, no dependencies.
26 lines
873 B
Common Lisp
26 lines
873 B
Common Lisp
;;; HASH TABLES 2: IS THE KEY THERE? (solution)
|
|
|
|
(defun has-key-p (key table)
|
|
(nth-value 1 (gethash key table)))
|
|
|
|
(defun lookup (key table default)
|
|
(multiple-value-bind (value present)
|
|
(gethash key table)
|
|
(if present value default)))
|
|
|
|
;;; ------------------------------------------------------------------
|
|
;;; Checks. Read them - they tell you what to aim for - but leave
|
|
;;; them unchanged.
|
|
;;; ------------------------------------------------------------------
|
|
|
|
(check (let ((table (make-hash-table)))
|
|
(setf (gethash 'a table) 1
|
|
(gethash 'b table) nil)
|
|
(and (has-key-p 'a table)
|
|
(has-key-p 'b table)
|
|
(not (has-key-p 'c table)))))
|
|
(check (let ((table (make-hash-table)))
|
|
(setf (gethash 'a table) 1)
|
|
(and (eql 1 (lookup 'a table 0))
|
|
(eql 99 (lookup 'c table 99)))))
|