Author Topic: Question on using APPLY  (Read 2005 times)

0 Members and 1 Guest are viewing this topic.

Jeremy

  • Guest
Question on using APPLY
« on: October 06, 2014, 03:27:25 PM »
I was trying to use APPLY on DEFUN to see if I could create a function by passing the elements in a list. Something along the lines of

(apply 'defun (list 'mytestfunc nil (princ "it works!")))

But if I try to run this it does not create MYTESTFUNC. How can one get this to work if at all?

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Question on using APPLY
« Reply #1 on: October 06, 2014, 03:36:32 PM »
Use LAMBDA or...

Code: [Select]
(eval (list 'defun 'mytestfunc nil (princ "it works!")))
What is the point of this?
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Question on using APPLY
« Reply #2 on: October 06, 2014, 03:41:45 PM »
Hi,

Code - Auto/Visual Lisp: [Select]
  1. (eval (cons 'defun (list 'mytestfunc nil '(princ "it works!"))))

Code - Auto/Visual Lisp: [Select]
  1. (setq mytestfunc (lambda () (princ "it works!")))

<EDIT> Oops, too slow...
Speaking English as a French Frog

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Re: Question on using APPLY
« Reply #3 on: October 06, 2014, 03:45:55 PM »
*excited* ...I know this one.

I had this from way back.

Code - Auto/Visual Lisp: [Select]
  1. (defun hook (func)
  2.   ;; hook
  3.   ;;
  4.   ;; this function will take an argument and turn it into a
  5.   ;; lambda expression to evaluate. (You can assign to a variable
  6.   ;; and exec it at a later time if you wish.)
  7.   ;;
  8.   ;; Ex: (hook '(+ 1 2))
  9.   ;;     > ((LAMBDA nil (+ 1 2)))
  10.   ;;    
  11.   ;; Ex 2: (setq zoom
  12.   ;;            (hook '(vla-zoomextents
  13.   ;;                (vlax-get-acad-object))))
  14.   ;;     > ((LAMBDA nil (vla-ZoomExtents (vlax-get-acad-object))))
  15.   ;;
  16.   ;; By: John K
  17.   ;;    (inspired by something I saw in one of
  18.   ;;     Vladimir Nesterovsky's procedures.)
  19.   ;;
  20.   (list (cons 'lambda (cons nil (list func)))) )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Jeremy

  • Guest
Re: Question on using APPLY
« Reply #4 on: October 06, 2014, 07:34:24 PM »
Once again the answer is the root of all EVAL ^-^ . So why is APPLY not sufficient? Is it not evaluating the function as is? Thanx for all the responses by the way.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Question on using APPLY
« Reply #5 on: October 07, 2014, 06:02:26 AM »
So why is APPLY not sufficient? Is it not evaluating the function as is?

Certain functions (usually Special Forms such as cond / foreach / defun etc.) cannot be evaluated using apply due to the difference in the way in which their arguments are evaluated.