【发布时间】:2011-02-16 16:42:48
【问题描述】:
我是函数式语言和 clojure 的新手,所以请多多包涵……
我正在尝试使用随机参数或常量构建函数列表。构造函数列表的函数已经在工作,尽管它不返回函数本身。我使用 println 验证了这一点。
(编辑:好的,毕竟它还不能正常工作)
(编辑:现在它正在工作,但它不能是“eval”-ed。看来我需要至少重复两次,以确保至少有两个子节点。这可能吗?)
这里是sn-p:
(def operations (list #(- %1 %2) #(+ %1 %2) #(* %1 %2) #(/ %1 %2)))
(def parameters (list \u \v \w \x \y \z))
(def parameterlistcount 6)
(def paramcount 2)
(def opcount 4)
(defn generate-function
([] (generate-function 2 4 0.5 0.6 () parameters))
([pc maxdepth fp pp function-list params]
(if (and (pos? maxdepth) (< (rand) fp))
(let [function-list
(conj function-list
(nth operations
(rand-int (count operations))))]
(recur pc (dec maxdepth) fp pp function-list params))
(if (and (< (rand) pp) (pos? pc))
(let [ params (pop parameters)
function-list
(conj function-list
(nth params
(rand-int (count params))))]
(if (contains? (set operations) (last function-list) )
(recur (dec pc) maxdepth fp pp function-list params)
nil))
(let [function-list
(conj function-list
(rand-int 100))]
(if (or (pos? maxdepth) (pos? pc))
(if (contains? (set operations) (last function-list) )
(recur pc maxdepth fp pp function-list params)
nil)
function-list))))))
任何帮助将不胜感激,谢谢!
【问题讨论】:
标签: functional-programming recursion lisp clojure