Files
lisplings/exercises/14_packages/pkg5.lisp
T

52 lines
1.4 KiB
Common Lisp

;;; PACKAGES 5: INTERNALS, NICKNAMES, SHADOWING
;;;
;;; A single colon reaches only EXPORTED symbols. The double colon
;;; shop::secret-sale reaches the internal ones too. :NICKNAMES
;;; gives a package extra names to call it by. And :SHADOW keeps a
;;; name a package USEs from colliding with your own - without it,
;;; defining FILL below would quietly redefine COMMON-LISP's FILL
;;; for everyone.
(defpackage #:shop
(:use #:common-lisp)
(:nicknames #:store)
(:export #:price))
(in-package #:shop)
(defun price ()
5)
(defun secret-sale ()
2)
(in-package #:common-lisp-user)
;; TODO: The secret sale price SHOP keeps for regulars only.
(defun best-deal ()
(shop:price))
;; TODO: The defpackage below is missing its (:shadow #:fill).
;; Add it, or DEFUN FILL under POST-OFFICE redefines COMMON-LISP's
;; FILL for everyone.
(defpackage #:post-office
(:use #:common-lisp))
(in-package #:post-office)
(defun fill (box)
(list 'stuffed box))
(in-package #:common-lisp-user)
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 5 (store:price))
(check-equal 2 (best-deal))
(check-equal (list 0 0 0) (fill (list 1 2 3) 0))
(check-equal '(post-office::stuffed crate)
(funcall (find-symbol "FILL" '#:post-office) 'crate))