Is it a function?

Discussion in 'AutoCAD' started by David Kozina, Jul 26, 2004.

  1. David Kozina

    David Kozina Guest

    Is there an easy way to determine if something is a function?

    Before reading below of one possible solution, I was wondering if any of you
    may know of another, possibly simpler way to do this.

    The basis for the question relates to Reini Urban's STDLIB functions.
    I have used and relied on these function in the past, but I would like to
    avoid continued dependency on them, as I don't know if or for how long they
    may continue to be maintained.

    One STDLIB function Reini has kindly provided is:
    (STD-MAKE-LIST n def)

    What it does is to create a static list of n def(ault items).
    (std-make-list 5 'A) -> (A A A A A)

    But something ELSE it can do is create a dynamic list - like so:
    (setq i 3)
    (std-make-list 5 '(lambda () (setq i (1+ i)))) -> (8 7 6 5 4)

    The difference in the output relates to the def(ault) argument.
    If the argument is a *function*, the list is evaluated dynamically.
    If the argument is not a function, a static list is returned.

    Thus my question: Is there an easy way to determine if something is a
    function?

    SPOILER:
    Reini's solution to this seems rather complex (read: difficult for me to
    understand).
    If you wish to see his solution, look for the STD-FUNCTIONP defun (beginning
    around line 330) here:
    http://xarch.tu-graz.ac.at/autocad/stdlib/STDINIT.LSP

    I don't know if there is any simpler way to do this.
    Which is why I'm asking here.

    Any thoughts?

    Best regards,
    David Kozina
     
    David Kozina, Jul 26, 2004
    #1
  2. David Kozina

    Tom Smith Guest

    Is there an easy way to determine if something is a function?

    (eq 'SUBR (type something))?
     
    Tom Smith, Jul 26, 2004
    #2
  3. David Kozina

    David Kozina Guest

    Well...
    Command: (eq 'SUBR (type (lambda () (setq i (1+ i)))))
    T

    because:
    Command: (type (lambda () (setq i (1+ i))))
    SUBR

    So far, so good.

    However, since...
    Command: (type '(lambda () (setq i (1+ i))))
    LIST

    The result becomes...
    Command: (eq 'SUBR (type '(lambda () (setq i (1+ i)))))
    nil

    ....and I need it to return T here (due to usage mentioned in previous post)

    I believe your test would be the biggest part of the solution, however.

    Best regards,
    David Kozina
     
    David Kozina, Jul 26, 2004
    #3
  4. (or (eq 'SUBR (type something))(eq 'EXRXSUBR (type something))(eq 'USUBR
    (type something)))

    This is more comprehensive, since there are different ways of defining
    functions. Unfortunately, it does not address David's problem with the
    lambda function.
     
    Randy Richardson, Jul 26, 2004
    #4
  5. David Kozina

    Tom Smith Guest

    Reini's solution seems pretty comprehensive, but it depends in small ways on
    four or five other stdlib functions. I think he handles David's problem
    with:

    (and
    (listp f)
    (eq (car f) 'LAMBDA)
    )
     
    Tom Smith, Jul 26, 2004
    #5
  6. David Kozina

    ECCAD Guest

    Anne,
    This one is out of sync as well.
    Bob
     
    ECCAD, Jul 26, 2004
    #6
  7. David Kozina

    Anne Brown Guest

    Thanks Bob. Reported it to the web team.

    Anne
     
    Anne Brown, Jul 26, 2004
    #7
  8. David Kozina

    David Kozina Guest

    Tom (and Randy),

    Thanks.

    I've found Reini's std-functionp (built in to the std-make-list function)
    helpful for some coding situations - I couldn't really figure any way to
    improve on it (I do think some parts harken back to R12 versions, though -
    so it might be shortened somewhat).

    I guess I was especially curious if any of the newer (and mostly unknown for
    me) vl* functions could open up other avenues of simplifying things. Looks
    as if not, however, as you both seemed to be reaching similar conclusions
    rather rapidly... :)

    Best regards,
    David Kozina
     
    David Kozina, Jul 27, 2004
    #8
  9. David Kozina

    Tom Smith Guest

    I guess I was especially curious if any of the newer (and mostly unknown
    for
    I'm a vl ignoramus, so don't rule it out on my account! That does seem like
    a possibility.
     
    Tom Smith, Jul 27, 2004
    #9
  10. David Kozina

    Josh Guest

    I don't if this offers any solution for you:

    Command: (type (EVAL '(lambda () (setq i (1+ i)))))
    SUBR
     
    Josh, Jul 27, 2004
    #10
  11. David Kozina

    David Kozina Guest

    Hmmm good point... how 'bout this then?:

    Code:
    
    (defun djkFunctionP
    (_func ; _func to check, Return T if _func is a function, nil otherwise
    /
    ;
    )
    ; begin
    (or
    (eq (type _func) 'SUBR)
    (eq (type _func) 'USUBR)
    (eq (type _func) 'EXRXSUBR)
    (and
    (not (eq _func T))
    (listp _func)
    (djkFunctionP (eval _func))
    )
    (and
    (not (eq _func T))
    (eq (type _func) 'SYM)
    (djkFunctionP (vl-symbol-value _func))
    )
    )
    ; end
    );_end defun
    
    
    Good? Bad? Ugly?
    (Why?)

    Best regards,
    David Kozina
     
    David Kozina, Jul 27, 2004
    #11
  12. David Kozina

    David Kozina Guest

    Small change (changed line 12)
    - previous code would choke using: (djkFunctionP nil)

    Code:
    
    (defun djkFunctionP
    (_func ; _func to check, Return T if _func is a function, nil otherwise
    /
    ;
    )
    ; begin
    (or
    (eq (type _func) 'USUBR)
    (eq (type _func) 'SUBR)
    (eq (type _func) 'EXRXSUBR)
    (and
    _func
    (listp _func)
    (djkFunctionP (eval _func))
    )
    (and
    (not (eq _func T))
    (eq (type _func) 'SYM)
    (djkFunctionP (vl-symbol-value _func))
    )
    )
    ; end
    );_end defun
    
    
    Anything else I'm overlooking?
     
    David Kozina, Jul 27, 2004
    #12
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.