home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / lisp / 2330 < prev    next >
Encoding:
Text File  |  1992-08-29  |  2.2 KB  |  48 lines

  1. Newsgroups: comp.lang.lisp
  2. Path: sparky!uunet!elroy.jpl.nasa.gov!ufo!Aig.Jpl.Nasa.Gov!charest
  3. From: charest@Aig.Jpl.Nasa.Gov (Len Charest)
  4. Subject: Re: A question: Dynamic type check
  5. Message-ID: <1992Aug28.192233.20104@jpl-devvax.jpl.nasa.gov>
  6. Sender: usenet@jpl-devvax.jpl.nasa.gov (For NNTP so rrn will be able to post)
  7. Nntp-Posting-Host: ai-cyclops
  8. Reply-To: charest@aig.jpl.nasa.gov
  9. Organization: NASA/Jet Propulsion Laboratory
  10. References:  <10026@uqcspe.cs.uq.oz.au>
  11. Date: Fri, 28 Aug 1992 19:22:33 GMT
  12. Lines: 34
  13.  
  14. In article <10026@uqcspe.cs.uq.oz.au>, gong@cs.uq.oz.au (Ming Gong) writes:
  15. |> I want to know *at run time* whether or not a expression is a function. 
  16. |> Can some knowledgeable people tell me how?
  17.  
  18. If the expression is bound to a variable X then
  19.     (functionp x)
  20. should do the job.
  21.  
  22. |> For example, I have a lisp function PLUS implementing 
  23. |>      lambda x. lambda y. x + y
  24. |> then (funcall (PLUS) 3) is a function and will printout something like
  25. |>      #<Function PLUS ....>.
  26.  
  27. Some observations: if PLUS is a Lisp function then the form (PLUS) is a call to that function with no arguments; furthermore (funcall (PLUS) 3) is a call to the *result* of (PLUS) with the argument 3. I'll assume that you mean (PLUS) returns the function object and that object is what prints as #<Function PLUS ....>. This suggests that PLUS is not an ordinary Lisp function object, but rather some custom data structure that you implemented yourself.
  28.  
  29. |> The questions are: can I access such information within a lisp program
  30. |> and how?
  31.  
  32. If the functions in question are true Lisp function objects, then FUNCTIONP should work as described above. Otherwise, the answer depends on your implementation of these 'functions'. For example, if you used DEFSTRUCT to define the data structure then a type-discriminating predicate would be automatically created. E.g.,
  33.  
  34. (defstruct function
  35.   name
  36.   lambda-list
  37.   code)
  38.  
  39. > (setq test (make-function :name 'plus :lambda-list '(x y)
  40.                             :code '(+ x y)))
  41. TEST
  42. > (function-p test)
  43. T
  44. ..................................................
  45.                                   Len Charest, Jr.
  46.                  JPL Artificial Intelligence Group
  47.                           charest@aig.jpl.nasa.gov
  48.