1

I've been using the Rosalind problems to learn clojure and the code below is part of a solution for the mortal rabbits problem. My solution was to create a record called rabbit that would keep track of each rabbit. The rabbit record would be stored in a vector and I would loop over the months and at each month update the vector by adding new rabbits (add-rabbits) and filtering the dead ones (remove-rabbits).

This sounds straightforward enough but I have run into a problem that I can't seem to wiggle around: when I try to pass the updated vector back into the loop I run into an error and I'm looking to figure out whats going on. I'm sure its one of those errors that I'll back on and it will be super obvious but hey, thats what happens with a new language right? Any insights would be appreciated.

;define a rabbit record with birth and death months
(defrecord rabbit [born dies] )
; function that calculates number of living rabbits
(defn iter-rabbit2 [months lifemonths]
  (letfn [ (add-rabbits [month rabbits]
           ;add-rabbits looks for reproductively active rabbits and creates new rabbits
               (let [num-repr-rabbits (count (filter #(>= (+ month 1) (.born %)) rabbits))
                     new-rabbits (vec (repeat num-repr-rabbits (rabbit. (+ month 1) (+ month lifemonths 1))))]
                    (do
                     (println "rabbits" rabbits)  ; for debugging
                     (println "rabbits type " (type rabbits))
                     (if (seq new-rabbits)
                         (apply conj (vec rabbits) (vec new-rabbits))
                         (rabbits)))))

          (remove-rabbits [month rabbits]
          ;remove rabbits filters the rabbits vector of dying rabbits
                (let [livingrabbits (vec (filter #(not= month (.dies %)) rabbits ))]
                     (do
                       (println "remove rabbits rabbits" livingrabbits) ;for debugging
                       (println (type livingrabbits))
                       (livingrabbits)))) ]

   (loop [month 0
          lifemonth lifemonths
          rabbits  [(rabbit. month lifemonth)] ]

    (if (= month months)
        [rabbits]
        (recur (+ month 1)
               (+ lifemonth 1)
               ( ->> rabbits
                      (add-rabbits month)
                      (remove-rabbits month)
                      (apply vector)
                      ))))))
(iter-rabbit2 5 6)

The error i am getting is the following: ArityException Wrong number of args (0) passed to: PersistentVector
clojure.lang.AFn.throwArity (AFn.java:437)

2 Answers 2

3

At the end of remove-rabbits you have (livingrabbits) which calls livingrabbits as an IFn with 0 arguments. Above that you define livingrabbits as a vector filtered from rabbits. A vector when called as an IFn takes one or two arguments: the index to look up and return, and the optional default if that index does not exist.

Similarly at the end of add-rabbits you have a call to (rabbits). In the recursion at the bottom of the function it seems that rabbits should also be a vector, so it also is being called with too few arguments.

I assume that you didn't mean to invoke livingrabbits / rabbits, but instead to return them. You can likely simply remove the extraneous parens (unless you meant to put rabbits in a parent data structure, in which case you can replace the parens with square brackets).

1
  • That is exactly right. I actually ended up putting extra vec calls into the code because when I went to sue the vectors they were not vectors. Indeed, this is the the issue. I believe I should be able to streamline several parts of the code based on this suggestion.
    – zach
    Commented Dec 12, 2013 at 17:28
0

Can I make a few suggestions.

  • You don't need to create a record for every individual rabbit. All rabbits born in the same year behave the same way. You only need to count them.
  • Using your code, corrected as noisesmith suggests above, the numbers are wrong: they do not match the solution given on the problem page.
  • For problems like this, the Clojure idiom is to present the solution as a lazy sequence. You can find an excellent account of this in Chapter 4 of Programming Clojure by Halloway and Bedra. A good starting point for your problem is Christopher Grand's solution for the Fibonacci sequence.

Here it is (from memory).

(defn fibo-base []
  (let [step (fn [[a b]] [b (+ a b)])]
    (map first (iterate step [0 1]))))

In general, I find it easier to learn with problems for which Clojure solutions are available. The following are helpful:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.