我构造了一个函数,命名find-all
为通过“递归”查找系列中给定项目的所有索引。第一次调用find-all
给出正确的输出。但是,从第二次调用开始,所有输出都附加在一起。
find-all: function [series found][
result: []
either any [empty? series none? s-found: find series found]
[result]
[append result index? s-found
find-all next s-found found]
]
;; test:
probe find-all "abcbd" "b" ;; output [2 4] as expected
probe find-all [1 2 3 2 1] 2 ;; output [2 4 2 4]
既然用创建的函数内部的变量function
是局部的,为什么result
在以后的函数调用中变量的值仍然存在,导致第二result
次调用的find-all
不是以 开头[]
?实现此功能的正确递归方式是什么?