Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lisp 在Jess中对数字列表求和_Lisp_Jess - Fatal编程技术网

Lisp 在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

我试图在Jess中列出一系列数字,但我不确定该怎么做:

(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))))
第二种可能会更有效