Lisp-如何在另一个函数中调用函数?

Lisp-如何在另一个函数中调用函数?,lisp,autocad,autolisp,Lisp,Autocad,Autolisp,我正在尝试调用以下函数: (defun c:Add () (setq a (getint "Enter a number to add 2 to it")) (setq a (+ a 2)) ) (defun LOOPER (func) ;repeats 'func' until user enters 'no' (setq dummy "w") (while dummy (func) ;this is obviously the pro

我正在尝试调用以下函数:

(defun c:Add ()
    (setq a (getint "Enter a number to add 2 to it"))
    (setq a (+ a 2))
)
(defun LOOPER (func)
    ;repeats 'func' until user enters 'no'
    (setq dummy "w")
    (while dummy
        (func) ;this is obviously the problem
        (setq order (getstring "\nContinue? (Y or N):"))
        (if (or (= order "N") (= order "n")) (setq dummy nil))
    )
)   
(defun c:add ( / a )
    (if (setq a (getint "\nEnter a number to add 2 to it: "))
        (+ a 2)
    )
)

(defun looper ( func )
    (while
        (progn
            (initget "Y N")
            (/= "N" (getkword "\nContinue? [Y/N] <Y>: "))
        )
        ((eval func))
    )
)

(defun c:adder ( )
    (looper 'c:add)
)
在该活套功能内:

(defun c:Add ()
    (setq a (getint "Enter a number to add 2 to it"))
    (setq a (+ a 2))
)
(defun LOOPER (func)
    ;repeats 'func' until user enters 'no'
    (setq dummy "w")
    (while dummy
        (func) ;this is obviously the problem
        (setq order (getstring "\nContinue? (Y or N):"))
        (if (or (= order "N") (= order "n")) (setq dummy nil))
    )
)   
(defun c:add ( / a )
    (if (setq a (getint "\nEnter a number to add 2 to it: "))
        (+ a 2)
    )
)

(defun looper ( func )
    (while
        (progn
            (initget "Y N")
            (/= "N" (getkword "\nContinue? [Y/N] <Y>: "))
        )
        ((eval func))
    )
)

(defun c:adder ( )
    (looper 'c:add)
)
像这样:

(defun c:Adder ()
    (LOOPER (c:Add))
)

如何避免
func
LOOPER
函数中未定义这一事实?

据我所知,您不能将函数名作为参数发送,但这里我给您一个类似的技术

我的机器上没有安装Autocad,因此无法测试此代码。但是如果有任何小错误,您可以删除,或者抓住这个概念,这样您就可以实现自己的

(defun c:Add ()
    (setq a (getint "Enter a number to add 2 to it"))
    (setq a (+ a 2))
)

(defun c:sub ()
    (setq a (getint "Enter a number to substract from 2:"))
    (setq a (-2 a))
)

(defun c:mul ()
    (setq a (getint "Enter a number to multiply with 2:"))
    (setq a (* a 2))
)


;----This function use to call other function from function name
;----Function name string is case sensitive
;----As per need you can Add function name to this function
(Defun callFunction(name)
(setq output nil)
;here you can add nested if condition but for simplicity I use If alone
(if (= name "C:Add")(setq output (C:Add)))
(if (= name "C:sub")(setq output (C:sub)))
(if (= name "C:mul")(setq output (C:mub)))
output
)

;----------Function end here



(defun LOOPER (func)
    ;repeats 'func' until user enters 'no'
    (setq dummy "w")
    (while dummy
        (callFunction func) ;Change here
        (setq order (getstring "\nContinue? (Y or N):"))
        (if (or (= order "N") (= order "n")) (setq dummy nil))
    )
)
您喜欢这样运行此程序:

(defun c:Adder ()
    (LOOPER ("c:Add"))
)

(defun c:substaker ()
    (LOOPER ("c:sub"))
)

(defun c:multiplyer ()
    (LOOPER ("c:mul"))
)

希望这有帮助:

据我所知,您不能将函数名作为参数发送,但这里我给您一个类似的技术

我的机器上没有安装Autocad,因此无法测试此代码。但是如果有任何小错误,您可以删除,或者抓住这个概念,这样您就可以实现自己的

(defun c:Add ()
    (setq a (getint "Enter a number to add 2 to it"))
    (setq a (+ a 2))
)

(defun c:sub ()
    (setq a (getint "Enter a number to substract from 2:"))
    (setq a (-2 a))
)

(defun c:mul ()
    (setq a (getint "Enter a number to multiply with 2:"))
    (setq a (* a 2))
)


;----This function use to call other function from function name
;----Function name string is case sensitive
;----As per need you can Add function name to this function
(Defun callFunction(name)
(setq output nil)
;here you can add nested if condition but for simplicity I use If alone
(if (= name "C:Add")(setq output (C:Add)))
(if (= name "C:sub")(setq output (C:sub)))
(if (= name "C:mul")(setq output (C:mub)))
output
)

;----------Function end here



(defun LOOPER (func)
    ;repeats 'func' until user enters 'no'
    (setq dummy "w")
    (while dummy
        (callFunction func) ;Change here
        (setq order (getstring "\nContinue? (Y or N):"))
        (if (or (= order "N") (= order "n")) (setq dummy nil))
    )
)
您喜欢这样运行此程序:

(defun c:Adder ()
    (LOOPER ("c:Add"))
)

(defun c:substaker ()
    (LOOPER ("c:sub"))
)

(defun c:multiplyer ()
    (LOOPER ("c:mul"))
)

希望这有帮助:

您可以将一个函数作为参数传递给另一个函数,如下例所示:

(defun c:add ( / a )
    (if (setq a (getint "\nEnter a number to add 2 to it: "))
        (+ a 2)
    )
)

(defun looper ( func )
    (while
        (progn
            (initget "Y N")
            (/= "N" (getkword "\nContinue? [Y/N] <Y>: "))
        )
        (func)
    )
)

(defun c:adder ( )
    (looper c:add)
)

将带引号的符号作为函数参数传递与标准AutoLISP函数更为一致,例如
mapcar
apply
等。

您可以将函数作为参数传递给另一个函数,如下例所示:

(defun c:add ( / a )
    (if (setq a (getint "\nEnter a number to add 2 to it: "))
        (+ a 2)
    )
)

(defun looper ( func )
    (while
        (progn
            (initget "Y N")
            (/= "N" (getkword "\nContinue? [Y/N] <Y>: "))
        )
        (func)
    )
)

(defun c:adder ( )
    (looper c:add)
)

将带引号的符号作为函数参数传递与标准AutoLISP函数更为一致,例如
mapcar
apply
等。

调用
(LOOPER(c:Add))
一次调用
(c:Add)
得到的号码是否会调用
LOOPER
?这与将函数
c:Add
作为参数传递不同。调用
(LOOPER(c:Add))
一次调用
(c:Add)
后,是否会调用
LOOPER
?这与将函数
c:Add
作为参数传递不同。你是一个传奇人物。谢谢你是个传奇人物。谢谢@李麦太好了,我将实施这项技术。@李麦太好了,我将实施这项技术。