[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

understand designator

james

7/21/2015 9:21:00 AM

I just find a interesting thing in lisp which is string-designator.

Here is my test code:
(defpackage :a.com (:use :cl)(:export :add))

(in-packate :a.com) works
(in-package "A.COM") also works.

I find the in-package will take string-designator parameter. So I understand the string-designator's usage. It will accept string,char,symbol as a parameter. Which is like a super-class type in java. The benefit is that the compiler will do parameter check. Did my understood right?

Also, how could I define a function which will use string-designator as a parameter?
Is that possible to create designator for user create class?

4 Answers

Pascal J. Bourguignon

7/21/2015 9:40:00 AM

0

james <dinglei2008@gmail.com> writes:

> I just find a interesting thing in lisp which is string-designator.
>
> Here is my test code:
> (defpackage :a.com (:use :cl)(:export :add))
>
> (in-packate :a.com) works
> (in-package "A.COM") also works.
>
> I find the in-package will take string-designator parameter. So I
> understand the string-designator's usage. It will accept
> string,char,symbol as a parameter. Which is like a super-class type in
> java. The benefit is that the compiler will do parameter check. Did my
> understood right?
>
> Also, how could I define a function which will use string-designator as a parameter?
> Is that possible to create designator for user create class?

Well, in Common Lisp, there's the distinction between types (which are
sets of values) and classes (which are how values are represented or
implemented).

string-designator is an informal type.
You could define:

(deftype string-designator () `(or string symbol character))

But the superclasses of string are: vector, array, sequence, t
of symbol are: t
of character are: t

So string-designator is definitely not a superclass of those classes.

In your programs you can of course define functions taking string
designators:

(deftype string-designator () `(or string symbol character))

(defvar *objects* (make-hash-table :test (function equal)))

(defun get-object-named (name)
(check-type name string-designator) ; uses the type defined above.
(gethash (string name) *objects*)) ; STRING converts a string designator
; into a string.


The notion of designator is general. Every time you can use an object
instead of another object, the former object is a designator for the
later object.



So you can invent your own designators.

For example, you could want to designate lists, but you could say that
atoms will designate a singleton list containing only this atom.

(defun ensure-list (list-designator)
(if (listp list-designator)
list-designator ; a list designates itself
(list list-designator))) ; an atom designates a list of itself.

(defun print-list (list-designator)
(princ "(")
(dolist (element (ensure-list list-designator))
(print element))
(princ ")")
(terpri))


cl-user> (print-list 2)
(
2 )
nil
cl-user> (print-list '(1 2 3))
(
1
2
3 )
nil
cl-user>

--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

james

7/21/2015 9:55:00 AM

0

On Tuesday, July 21, 2015 at 5:40:14 PM UTC+8, informatimago wrote:
> james <dinglei2008@gmail.com> writes:
>
> > I just find a interesting thing in lisp which is string-designator.
> >
> > Here is my test code:
> > (defpackage :a.com (:use :cl)(:export :add))
> >
> > (in-packate :a.com) works
> > (in-package "A.COM") also works.
> >
> > I find the in-package will take string-designator parameter. So I
> > understand the string-designator's usage. It will accept
> > string,char,symbol as a parameter. Which is like a super-class type in
> > java. The benefit is that the compiler will do parameter check. Did my
> > understood right?
> >
> > Also, how could I define a function which will use string-designator as a parameter?
> > Is that possible to create designator for user create class?
>
> Well, in Common Lisp, there's the distinction between types (which are
> sets of values) and classes (which are how values are represented or
> implemented).
>
> string-designator is an informal type.
> You could define:
>
> (deftype string-designator () `(or string symbol character))
>
> But the superclasses of string are: vector, array, sequence, t
> of symbol are: t
> of character are: t
>
> So string-designator is definitely not a superclass of those classes.
>
> In your programs you can of course define functions taking string
> designators:
>
> (deftype string-designator () `(or string symbol character))
>
> (defvar *objects* (make-hash-table :test (function equal)))
>
> (defun get-object-named (name)
> (check-type name string-designator) ; uses the type defined above.
> (gethash (string name) *objects*)) ; STRING converts a string designator
> ; into a string.
>
>
> The notion of designator is general. Every time you can use an object
> instead of another object, the former object is a designator for the
> later object.
>
>
>
> So you can invent your own designators.
>
> For example, you could want to designate lists, but you could say that
> atoms will designate a singleton list containing only this atom.
>
> (defun ensure-list (list-designator)
> (if (listp list-designator)
> list-designator ; a list designates itself
> (list list-designator))) ; an atom designates a list of itself.
>
> (defun print-list (list-designator)
> (princ "(")
> (dolist (element (ensure-list list-designator))
> (print element))
> (princ ")")
> (terpri))
>
>
> cl-user> (print-list 2)
> (
> 2 )
> nil
> cl-user> (print-list '(1 2 3))
> (
> 1
> 2
> 3 )
> nil
> cl-user>
>
> --
> __Pascal Bourguignon__ http://www.informat...
> "The factory of the future will have only two employees, a man and a
> dog. The man will be there to feed the dog. The dog will be there to
> keep the man from touching the equipment." -- Carl Bass CEO Autodesk

I am impressed by the answer and reply speed. Do you have a machine to answer question automatically :)? Need more time to understand this.

Kaz Kylheku

7/21/2015 1:54:00 PM

0

On 2015-07-21, james <dinglei2008@gmail.com> wrote:
> I just find a interesting thing in lisp which is string-designator.
>
> Here is my test code:
> (defpackage :a.com (:use :cl)(:export :add))
>
> (in-packate :a.com) works
> (in-package "A.COM") also works.
>
> I find the in-package will take string-designator parameter. So I understand
> the string-designator's usage. It will accept string,char,symbol as a
> parameter. Which is like a super-class type in java. The benefit is that the
> compiler will do parameter check. Did my understood right?

The obvious benefit is that you can use a symbol or string when referring
to a package.

Note that the package system *underlies* the symbol system, and so it would be
somewhat inappropriate to require packages to be designated by symbols.

> Also, how could I define a function which will use string-designator as a
> parameter? Is that possible to create designator for user create class?

"string-designator" is not a type; it's a convention. Some symbol-related
API's in Lisp can take a string or a symbol.

For instance, API's that refer to a package can accept its string name,
or a symbol in the place of a string.

This doesn't mean that there exists a designator class type.

In Lisp, we can easily make completely unrelated types provide the same (or
closely-related) functionality. We can do this in using Lisp's object system
via generic functions and methods, or via explicit type tests.

;; use typecase:

(defun myfunc (whatever)
(typecase whatever
(string ... code for string case ...)
(symbol ... code for symbol case ...)
(t (error ...))))


;; use CLOS:

(defgeneric myfunc (whatever))

(defmethod myfunc ((whatever string))
;; ... code for string case ...
)

(defmethod myfunc ((whatever symbol))
;; ... code for symbol case: e.g. recurse to string case:
(myfunc (symbol-name symbol)))

;; other types handled implicitly via "no applicable method" error.
;; or else we can do:

(defmethod myfunc (whatever) ;; same as ((whatever t))
(error "myfunc: ... error message here "))

james

7/24/2015 1:32:00 AM

0

On Tuesday, July 21, 2015 at 9:53:53 PM UTC+8, Kaz Kylheku wrote:
> On 2015-07-21, james <dinglei2008@gmail.com> wrote:
> > I just find a interesting thing in lisp which is string-designator.
> >
> > Here is my test code:
> > (defpackage :a.com (:use :cl)(:export :add))
> >
> > (in-packate :a.com) works
> > (in-package "A.COM") also works.
> >
> > I find the in-package will take string-designator parameter. So I understand
> > the string-designator's usage. It will accept string,char,symbol as a
> > parameter. Which is like a super-class type in java. The benefit is that the
> > compiler will do parameter check. Did my understood right?
>
> The obvious benefit is that you can use a symbol or string when referring
> to a package.
>
> Note that the package system *underlies* the symbol system, and so it would be
> somewhat inappropriate to require packages to be designated by symbols.
>
> > Also, how could I define a function which will use string-designator as a
> > parameter? Is that possible to create designator for user create class?
>
> "string-designator" is not a type; it's a convention. Some symbol-related
> API's in Lisp can take a string or a symbol.
>
> For instance, API's that refer to a package can accept its string name,
> or a symbol in the place of a string.
>
> This doesn't mean that there exists a designator class type.
>
> In Lisp, we can easily make completely unrelated types provide the same (or
> closely-related) functionality. We can do this in using Lisp's object system
> via generic functions and methods, or via explicit type tests.
>
> ;; use typecase:
>
> (defun myfunc (whatever)
> (typecase whatever
> (string ... code for string case ...)
> (symbol ... code for symbol case ...)
> (t (error ...))))
>
>
> ;; use CLOS:
>
> (defgeneric myfunc (whatever))
>
> (defmethod myfunc ((whatever string))
> ;; ... code for string case ...
> )
>
> (defmethod myfunc ((whatever symbol))
> ;; ... code for symbol case: e.g. recurse to string case:
> (myfunc (symbol-name symbol)))
>
> ;; other types handled implicitly via "no applicable method" error.
> ;; or else we can do:
>
> (defmethod myfunc (whatever) ;; same as ((whatever t))
> (error "myfunc: ... error message here "))

Thanks. My feeling is there are so many unknown details for me in learning lisp but interesting. Thanks for such a good community here.