The following is a recursive function:
(defn make-control-data [it alphabet]
{:pre [(integer? it) (pos? it)]}
(let [shuffled-alphabet-string (reduce str (shuffle alphabet))]
(if (zero? it)
shuffled-alphabet-string
(str shuffled-alphabet-string (make-control-data (dec it) alphabet)))))
It should take an integer (it) that specifies the number recursive calls and a string list of letters, e.g., ["a" "b"]. It should return a randomly ordered string of length it*length(alphabet) using all the letters from the alphabet. If it = 2 and the alphabet = ["a" "b"] the function should yield a random string of length (* 2 (count ["a" "b"])) = 4 using all the letters from the alphabet ["a" "b"].
It breaks the pre condition (pos? it), and the returns a string of length (it+1)*length(alphabet).
Can anyone see what’s wrong?
Your function is apparently willing to accept
it=0, so your precondition should not forbid that input. Zero is your recursive base case, not an error. If I were putting a precondition on this function (though I wouldn’t), it would be[(not (neg? it))].If I were writing this from scratch I wouldn’t bother with all the recursive noise, but simply: