Search code examples
clojureshortest-path

Clojure - wrong number of args passed to doall


I'm currently working on a route planning robot and I'm getting an error with passing the wrong number of arguments to doall.

(defn multipleparcels [parcel]
  (let [newparcel (first parcel)
   start (:start newparcel)
   end (:end newparcel)
   delivery (:delivery newparcel)]
  (if (empty? parcel)
    (println "Deliveries Completed")
    (doall (journey start end)
    (if delivery
      (println "Parcel Delivered")
      (println "Parcel Collected")) 
  (multipleparcels (rest parcel))))))

I use this code in the following function.

(defn robotroute [robot]
  (let [parcels (:parcels robot)
  numstops (:numStops robot)]
  (if (= 1 numstops)
    (oneparcel parcels)
    (multipleparcels parcels))
   (calccost parcels 0)))

Then I make use of these functions with the following code:

(def task3parcel [(Parcel. :main-office :r113 false)
                  (Parcel. :r113 :r115 true)])
(def task3robot (Robot. task3parcel 2))
(def task3 (robotroute task3robot)

The code runs through and outputs all the correct information. However right at the end I get the following error.

CompilerException clojure.lang.ArityException: Wrong number of args (3) passed to: core/doall, compiling:(form-init9046500356350698733.clj:1:12) 

The error is stopping my calccost code from running through. Can anyone see where this error will be coming from? I have tried moving brackets round etc. However I've been unable to get anything working thus far.

Can anyone see where this error is coming from? If so does anyone have any tips for fixing it?

Edit: Implementing suggested answer.

(defn multipleparcels [parcel]
  (let [newparcel (first parcel)
   start (:start newparcel)
   end (:end newparcel)
   delivery (:delivery newparcel)]
  (if (empty? parcel)
    (println "Deliveries Completed")
    (doall (journey start end))
    (if delivery
      (println "Parcel Delivered")
      (println "Parcel Collected")) 
  (multipleparcels (rest parcel)))))

Solution

  • As the error states you are passing three arguments to doall instead of the one expected. This is due to a misplaced parenthesis. The containing if expression also contains three arguments instead of the one or two expected. If you want to execute some side-effects as part of an expression, use do:

    (if (empty? parcel)
        (do
          (println "Deliveries Completed")
          (doall (journey start end)))
          (if delivery
            (println "Parcel Delivered")
            (println "Parcel Collected")))
    

    Be aware that you are discarding the evaluated sequence returned by (journey start end)