TwirlwT
12/31/2015 12:46:00 PM
El jueves, 31 de diciembre de 2015, 7:08:32 (UTC+1), informatimago escribió:
> Following an irc discussion, here is an example, of package use where
> we have:
>
> - an interface package exporting a public API (list of symbols),
> - an implementation package implementing this public API,
> - a client package using this public API.
>
> This interface package doesn't :use any other package (it could :use
> other interface packages if it extended them).
>
> ;;------------------------------------------------------------------------
>
> (defpackage "FACTORIAL-INTERFACE"
> (:use)
> (:export "FACTORIAL"))
>
> ;;------------------------------------------------------------------------
>
> (defpackage "FACTORIAL-IMPLEMENTATION"
> ;; what we implement:
> (:use "FACTORIAL-INTERFACE")
> ;; how we implement it:
> (:use "COMMON-LISP"))
>
> (in-package "FACTORIAL-IMPLEMENTATION")
>
> (defun factorial (x)
> (check-type x (real 0))
> (if (plusp x)
> (* x (factorial (1- x)))
> 1))
>
> ;;------------------------------------------------------------------------
>
> (defpackage "FACTORIAL-CLIENT"
> ;; entry point:
> (:export "MAIN")
> ;; how we implement it:
> (:use "COMMON-LISP")
> (:use "FACTORIAL-INTERFACE"))
>
> (in-package "FACTORIAL-CLIENT")
>
> (defun main ()
> (format *query-io* "Enter a positive real: ")
> (finish-output *query-io*)
> (let* ((x (read *query-io*))
> (x! (factorial x)))
> (format t "~A! = ~A~%" x x!)
> (finish-output)))
>
> ;;------------------------------------------------------------------------
>
>
> cl-user> (factorial-client:main)
> Enter a positive real: 42
> 42! = 1405006117752879898543142606244511569936384000000000
> nil
> cl-user>
>
>
> --
> __Pascal Bourguignon__
>
> Croire en l'histoire officielle, c'est croire des criminels sur parole.
> -- Simone WEIL (1909-1943) philosophe Française.
Good example. Perhaps your example should be preceded by (in-package "COMMON-LISP").
And perhaps it should be remarked, to make the message more clear, that:
* (find-symbol "FACTORIAL" (find-package "FACTORIAL-INTERFACE"))
=> FACTORIAL-INTERFACE:FACTORIAL
:EXTERNAL
* (find-symbol "FACTORIAL" (find-package "FACTORIAL-IMPLEMENTATION"))
=> FACTORIAL-INTERFACE:FACTORIAL
:INHERITED
A wish you all (trolls included) a very happy new year, let's see what new exciting things this years bring to us. My aim for the new year is to get stronger and this not only for fighting trolls ))).
Sheers from heart.