【问题标题】:Lisp - How to call a function within another function?Lisp - 如何在另一个函数中调用一个函数?
【发布时间】:2018-12-02 23:39:22
【问题描述】:

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

(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 函数中未定义的事实?

【问题讨论】:

  • 不会(LOOPER (c:Add)) 调用LOOPER 并通过调用(c:Add) 得到number 一次吗?与将函数c:Add作为参数传递是不一样的。

标签: lisp autocad autolisp


【解决方案1】:

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

(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 等。

【讨论】:

  • @lee mac 太好了,我会实现这个技术。
【解决方案2】:

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

我的机器上没有安装 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"))
)

希望这会有所帮助:

【讨论】:

  • 你是传奇人物。谢谢!
猜你喜欢
  • 1970-01-01
  • 2018-07-02
  • 1970-01-01
  • 2011-05-30
  • 2019-09-17
  • 2013-12-17
  • 1970-01-01
  • 1970-01-01
  • 2017-04-07
相关资源
最近更新 更多