Lisp 在Jess中对数字列表求和
我试图在Jess中列出一系列数字,但我不确定该怎么做:Lisp 在Jess中对数字列表求和,lisp,jess,Lisp,Jess,我试图在Jess中列出一系列数字,但我不确定该怎么做: (deffunction sumAll ($?n) (return (+ ?n))) (sumAll 1 2 3) 上面的代码不起作用。我该怎么做呢?这里有两种方法。您可以通过将函数调用构建为字符串并使解析器重新解析它来执行一行程序: (deffunction sumAll($?args) (eval (str-cat "(+ " (implode$ ?args) ")" ))) 或者您可以显式地进行迭代 (deffuncti
(deffunction sumAll ($?n) (return (+ ?n)))
(sumAll 1 2 3)
上面的代码不起作用。我该怎么做呢?这里有两种方法。您可以通过将函数调用构建为字符串并使解析器重新解析它来执行一行程序:
(deffunction sumAll($?args)
(eval (str-cat "(+ " (implode$ ?args) ")" )))
或者您可以显式地进行迭代
(deffunction sumAll($?args)
(bind ?sum 0)
(foreach ?num ?args
(bind ?sum (+ ?sum ?num))))
第二种可能会更有效