How to return the output of a recursive function in Clojure

Posted by Silanglaya Valerio on Stack Overflow See other posts from Stack Overflow or by Silanglaya Valerio
Published on 2010-05-07T04:55:32Z Indexed on 2010/05/07 4:58 UTC
Read the original article Hit count: 378

Hi everyone! I'm new to functional languages and clojure, so please bear with me...

I'm trying to construct a list of functions, with either random parameters or constants. The function that constructs the list of functions is already working, though it doesn't return the function itself. I verified this using println.

Here is the 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 generateFunction
"Generates a random function list"
([] (generateFunction 2 4 0.5 0.6 '()))
([pc maxdepth fp pp function]
    (if (and (> maxdepth 0) (< (rand) fp))
      (dotimes [i 2]
 (println(conj (generateFunction pc (dec maxdepth) fp pp function)
       {:op (nth operations (rand-int opcount))})))
      (if (and (< (rand) pp) (> pc 0))
 (do (dec pc)
   (conj function {:param (nth parameters (rand-int parameterlistcount))}))
 (conj function {:const (rand-int 100)})))))

Any help will be appreciated, thanks!

© Stack Overflow or respective owner

Related posts about clojure

Related posts about recursion