How to return the output of a recursive function in Clojure

I am new to functional languages ​​and clojure, so please bear with me ...

I am trying to build a list of functions with arbitrary parameters or constants. A function that builds a list of functions already works, although the function itself does not return. I checked this with println.

(edit: ok, it doesn't work correctly yet)

(edit: it works now, but it cannot be "eval" -ed. It seems I need to repeat at least twice to make sure that there are at least two child nodes. Is this possible?)

Here is a snippet:

(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))))))

Any help would be appreciated, thanks!

+3
source share
3

(. ):

(defn generate-function
  ([] (generate-function 2 4 0.5 0.6 ()))
  ([pc maxdepth fp pp function-list]
     (if (and (pos? maxdepth) (< (rand) fp))
       (let [function-list
             (conj function-list
                   {:op
                    (nth operations
                         (rand-int (count operations)))})]
         (recur pc (dec maxdepth) fp pp function-list))
       (if (and (< (rand) pp) (pos? pc))
         (let [function-list
               (conj function-list
                     {:param
                      (nth parameters
                           (rand-int (count parameters)))})]
           (recur (dec pc) maxdepth fp pp function-list))
         (let [function-list
               (conj function-list
                     {:const
                      (rand-int 100)})]
           (if (or (pos? maxdepth) (pos? pc))
             (recur pc maxdepth fp pp function-list)
             function-list))))))

REPL...

user> (generate-function)
({:const 63} {:op #<user$fn__4557 user$fn__4557@6cbb2d>} {:const 77} {:param \w} {:op #<user$fn__4559 user$fn__4559@8e68bd>} {:const 3} {:param \v} {:const 1} {:const 8} {:op #<user$fn__4559 user$fn__4559@8e68bd>} {:op #<user$fn__4555 user$fn__4555@6f0962>})
user> (generate-function)
({:const 27} {:param \y} {:param \v} {:op #<user$fn__4561 user$fn__4561@10463c3>} {:op #<user$fn__4561 user$fn__4561@10463c3>} {:op #<user$fn__4561 user$fn__4561@10463c3>} {:op #<user$fn__4561 user$fn__4561@10463c3>} {:const 61})

, , :

  • recur , . dotimes, , function-list generate-function. , recur , , ( , , , , ) .

  • (do (dec pc) ...) pc . ( , ) Clojure ; . pc, , maxdepth .

  • generate-function, Clojure. , , function, function-list ( , , , generate-function-list ... hm), , .

  • , opcount Var ; Clojure ( list) , (count some-list) - ( ). , operations parameters ( , !). . [\u \v \w \x \y \z].

  • Clojure 1.2 (rand-nth coll) (nth coll (rand-int (count coll))).

  • Clojure , ops, params , eval. , , .

  • op/param/constant: - {:category foo, :content bar}, foo :op, :param :const bar - foo.

+3

, Clojure (recur...) . : " , recur , , Clojure".

, , , , .

:

(let [n (rand-int 10)] 
  (println "Let define f with n =" n)
  (defn f [x] 
    (if (> x n) 
      "result" 
      (do (println x) 
          (recur (inc x))))))

:

Let define f with n = 4

user> (f 0)
0
1
2
3
4
"result"

4, , 0 () 10 ().

+2

, , . . , , 15 . > _ & ;

(defn generate-branch
"Generate branches for tree"
  ([] (generate-branch 0.6 () (list \x \y \z)))
  ([pp branch params]
      (loop [branch
        (conj branch (nth operations (rand-int (count operations))))]
    (if (= (count branch) 3)
      branch
      (if (and (< (rand) pp))
        (recur (conj branch (nth params (rand-int (count params)))))
        (recur (conj branch (rand-int 100))))))))

(defn build-vertex
"Generates a vertex from branches"
  []
  (let [vertex (list (nth operations (rand-int (count operations))))]
    (conj vertex (take 5 (repeatedly generate-branch)))))

!

+1
source

Source: https://habr.com/ru/post/1744449/


All Articles