首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >突变问题-- Clojure

突变问题-- Clojure
EN

Stack Overflow用户
提问于 2010-05-23 09:46:08
回答 1查看 400关注 0票数 0

更改表示为列表的函数的元素时遇到问题。

随机函数的代码:

代码语言:javascript
复制
(defn makerandomtree-10
[pc maxdepth maxwidth fpx ppx]
(if-let [output 
  (if (and (< (rand) fpx) (> maxdepth 0))
    (let [head (nth operations (rand-int (count operations)))
      children (doall (loop[function (list)
            width maxwidth]
              (if (pos? width)
            (recur (concat function 
              (list (makerandomtree-10 pc (dec maxdepth) 
                (+ 2 (rand-int (- maxwidth 1))) fpx ppx)))
              (dec width))
            function)))]
    (concat (list head) children))
    (if (and (< (rand) ppx) (>= pc 0))
    (nth parameters (rand-int (count parameters)))
    (rand-int 100)))]
output
))

我还将提供一个变异函数,它仍然不够好。我需要能够评估我的语句,所以以下内容仍然不够。

代码语言:javascript
复制
(defn mutate-5
"chooses a node changes that"
[function pc maxwidth pchange]
(if (< (rand) pchange)
    (let [output (makerandomtree-10 pc 3 maxwidth 0.5 0.6)]
    (if (seq? output) output (list output)))
    ;mutate the children of root
    ;declare an empty accumulator list, with root as its head 
    (let [head (list (first function))
          children (loop [acc(list)
                  walker (next function)] (println "----------") (println walker) (println "-----ACC-----") (println acc)
            (if (not walker)
                acc
                (if (or (seq? (first function)) (contains? (set operations) (first function)))
                  (recur (concat acc (mutate-5 walker pc maxwidth pchange))
                    (next walker))
                  (if (< (rand) pchange)
                    (if (some (set parameters) walker)
                    (recur (concat acc (list (nth parameters (rand-int (count parameters)))))
                        (if (seq? walker) (next walker) nil))
                    (recur (concat acc (list (rand-int 100)))
                        (if (seq? walker) (next walker) nil)))
                    (recur acc (if (seq? walker) (next walker) nil))))
             ))]
        (concat head (list children)))))

(附注:您是否有学习clojure的链接/书籍?)

EN

回答 1

Stack Overflow用户

发布于 2010-05-24 18:18:46

查看clojure.walk名称空间。像clojure.walk/postwalk-replace这样的函数很可能就是您要找的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2890432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档