From: Captain Obvious on
sd> So one cannot inspect a function, is that what's you're
sd> saying, Zach?

Common Lisp standard does not require functions to be inspectable.
Because Common Lisp is supposed to be compiled and be effective,
w.r.t. memory usage too, I guess.

But is is not a problem because you can extract information you
want via macro at time function is defined. Write your own defun-like
macro and capture whatever you want.

E.g. if you want some functions to be accessible via RPC of some sort,
you can make a macro defun-rpc which is pretty much like defun:

(defun-rpc 10+ (number) (+ 10 number))

But internally it counts parameters, publishes functions, whatever.

That is a Common Lisp way to do it.

sd> Have I fallen for an April fool's joke? No dice?

Some implementations provide functionality which is not required
by standard.


From: joswig on
On 1 Feb., 00:34, slash dot <tynty...(a)gmail.com> wrote:
> lisp is famous for is reflection capabilities,

Right. Though the capabilities vary between Lisp dialects and
implementations.

> and,
> indeed, a function like '10+' is reported to be
>
>     #<FUNCTION 10+ (NUMBER) (DECLARE (SYSTEM::IN-DEFUN 10+))
>     (BLOCK 10+ (+ 10 NUMBER))>
>
> in clisp.
>
> (Disturbingly, this only works if I bind #'10+ to a variable and
> evaluate
> the variable, but that's a clisp implementation detail, right? RIGHT?)
>
> So my question is: how can I inspect functions to find out how
> many arguments it has, for example?

If we talk about Common Lisp, the standard does not really give you
that
functionality. There is the function 'function-lambda-expression',
which
may work or not.

Individual Common Lisp implementations usually have a function
to get argument lists. Historically this function was often called
ARGLIST in some package. LispWorks calls it lw:function-lambda-list.

CL-USER 5 > (lw:function-lambda-list 'url:parse-internet-mail-address)
(STRING &OPTIONAL (URL::START 0) (URL::END (LENGTH STRING)))

In MCL

? (arglist #'print-object)
(OBJECT STREAM)
:DECLARATION




From: D Herring on
On 01/31/2010 06:34 PM, slash dot wrote:
> lisp is famous for is reflection capabilities, and,
> indeed, a function like '10+' is reported to be
>
> #<FUNCTION 10+ (NUMBER) (DECLARE (SYSTEM::IN-DEFUN 10+))
> (BLOCK 10+ (+ 10 NUMBER))>
>
> in clisp.
>
> (Disturbingly, this only works if I bind #'10+ to a variable and
> evaluate
> the variable, but that's a clisp implementation detail, right? RIGHT?)
>
> So my question is: how can I inspect functions to find out how
> many arguments it has, for example?

Whether you need to do this for a single implementation or you need to
do this portably, I would look at a library like Conium for inspiration.

http://gitorious.org/conium

- Daniel
From: Pascal Costanza on
On 01/02/2010 10:59, joswig(a)corporate-world.lisp.de wrote:
> On 1 Feb., 00:34, slash dot<tynty...(a)gmail.com> wrote:
>> lisp is famous for is reflection capabilities,
>
> Right. Though the capabilities vary between Lisp dialects and
> implementations.
>
>> and,
>> indeed, a function like '10+' is reported to be
>>
>> #<FUNCTION 10+ (NUMBER) (DECLARE (SYSTEM::IN-DEFUN 10+))
>> (BLOCK 10+ (+ 10 NUMBER))>
>>
>> in clisp.
>>
>> (Disturbingly, this only works if I bind #'10+ to a variable and
>> evaluate
>> the variable, but that's a clisp implementation detail, right? RIGHT?)
>>
>> So my question is: how can I inspect functions to find out how
>> many arguments it has, for example?
>
> If we talk about Common Lisp, the standard does not really give you
> that
> functionality. There is the function 'function-lambda-expression',
> which
> may work or not.
>
> Individual Common Lisp implementations usually have a function
> to get argument lists. Historically this function was often called
> ARGLIST in some package. LispWorks calls it lw:function-lambda-list.

A way to do this portably is by defining your own function-defining
forms. Here is a sketch (untested):

(in-package :closer-common-lisp-user) ;; requires CLOS MOP support

(defclass my-function (funcallable-standard-object)
((meta-information :initarg :meta :reader meta-information))
(:metaclass funcallable-standard-class))

(defmethod initialize-instance :after
((f my-function) &key function)
(set-funcallable-instance-function f function))

(defmacro my-defun (name (&rest lambda-list) &body body)
`(progn
(setf (fdefinition ',name)
(make-instance 'my-function
:function (lambda ,lambda-list ,@body)
:meta ',lambda-list))
',name))

(defmacro my-lambda ((&rest lambda-list) &body body)
(make-instance 'my-function
:function (lambda ,lambda-list ,@body)
:meta ',lambda-list))

You can use package shadowing to make sure that you use the same names
for symbols like the original definition forms from the CL package,
which makes it easier to use this with existing code.


Pascal


--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: joswig on
On 1 Feb., 14:57, Pascal Costanza <p...(a)p-cos.net> wrote:
> On 01/02/2010 10:59, jos...(a)corporate-world.lisp.de wrote:
>
>
>
>
>
> > On 1 Feb., 00:34, slash dot<tynty...(a)gmail.com>  wrote:
> >> lisp is famous for is reflection capabilities,
>
> > Right. Though the capabilities vary between Lisp dialects and
> > implementations.
>
> >> and,
> >> indeed, a function like '10+' is reported to be
>
> >>      #<FUNCTION 10+ (NUMBER) (DECLARE (SYSTEM::IN-DEFUN 10+))
> >>      (BLOCK 10+ (+ 10 NUMBER))>
>
> >> in clisp.
>
> >> (Disturbingly, this only works if I bind #'10+ to a variable and
> >> evaluate
> >> the variable, but that's a clisp implementation detail, right? RIGHT?)
>
> >> So my question is: how can I inspect functions to find out how
> >> many arguments it has, for example?
>
> > If we talk about Common Lisp, the standard does not really give you
> > that
> > functionality. There is the function 'function-lambda-expression',
> > which
> > may work or not.
>
> > Individual Common Lisp implementations usually have a function
> > to get argument lists. Historically this function was often called
> > ARGLIST in some package. LispWorks calls it lw:function-lambda-list.
>
> A way to do this portably is by defining your own function-defining
> forms. Here is a sketch (untested):
>
> (in-package :closer-common-lisp-user) ;; requires CLOS MOP support
>
> (defclass my-function (funcallable-standard-object)
>    ((meta-information :initarg :meta :reader meta-information))
>    (:metaclass funcallable-standard-class))
>
> (defmethod initialize-instance :after
>    ((f my-function) &key function)
>    (set-funcallable-instance-function f function))
>
> (defmacro my-defun (name (&rest lambda-list) &body body)
>    `(progn
>       (setf (fdefinition ',name)
>             (make-instance 'my-function
>               :function (lambda ,lambda-list ,@body)
>               :meta ',lambda-list))
>       ',name))
>
> (defmacro my-lambda ((&rest lambda-list) &body body)
>    (make-instance 'my-function
>      :function (lambda ,lambda-list ,@body)
>      :meta ',lambda-list))
>
> You can use package shadowing to make sure that you use the same names
> for symbols like the original definition forms from the CL package,
> which makes it easier to use this with existing code.
>
> Pascal
>
> --
> My website:http://p-cos.net
> Common Lisp Document Repository:http://cdr.eurolisp.org
> Closer to MOP & ContextL:http://common-lisp.net/project/closer/

I would avoid that, since most implementations
can return function arglists already. That's needed for
any inspector/describe/IDE feature showing a function's
arglist.

Just write a
function that calls the unportable implementation specific thing.
I would not invent a new mechanism without a real need.
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13
Prev: Do as the Romans do
Next: managing large table of data