Lisp - 如何在另一个函数中调用一个函数?
Lisp - How to call a function within another function?
我正在尝试调用以下函数:
(defun c:Add ()
(setq a (getint "Enter a number to add 2 to it"))
(setq a (+ a 2))
)
在这个 LOOPER 函数中:
(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: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"))
)
希望对您有所帮助:
您可以将一个函数作为参数传递给另一个函数,如下例所示:
(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)
)
这里,符号 c:add
被求值以产生指向函数定义的指针,然后它被绑定到 looper
函数范围内的符号 func
。因此,在 looper
函数的范围内,符号 func
和 c:add
计算相同的函数。
或者,您可以将符号 c:add
作为带引号的符号传递,在这种情况下,符号 func
的值是符号 c:add
,然后可以计算为产生函数:
(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)
)
将带引号的符号作为函数参数传递更符合标准 AutoLISP 函数,例如 mapcar
、apply
等。
我正在尝试调用以下函数:
(defun c:Add ()
(setq a (getint "Enter a number to add 2 to it"))
(setq a (+ a 2))
)
在这个 LOOPER 函数中:
(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: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"))
)
希望对您有所帮助:
您可以将一个函数作为参数传递给另一个函数,如下例所示:
(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)
)
这里,符号 c:add
被求值以产生指向函数定义的指针,然后它被绑定到 looper
函数范围内的符号 func
。因此,在 looper
函数的范围内,符号 func
和 c:add
计算相同的函数。
或者,您可以将符号 c:add
作为带引号的符号传递,在这种情况下,符号 func
的值是符号 c:add
,然后可以计算为产生函数:
(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)
)
将带引号的符号作为函数参数传递更符合标准 AutoLISP 函数,例如 mapcar
、apply
等。