Search code examples
clojure

Why does clojure give this arity error


I have the function definition below for map-edit

(def map-edit
  (fn [m lst k f]
    (if (car lst)
      (assoc m
             (car lst)
             (map-edit (get m (car lst) {}) k f))
      (assoc m k (f (get m k))))))

When I try to call this function in my repl

(map-edit {} (list "oeu") "oeuoeu" (fn [q] "oeu"))

I get an error for Arity

ArityException Wrong number of args (3) passed to: core/map-edit  clojure.lang.AFn.throwArity (AFn.java:429)

Why does it think I am only passing 3 arguments?

; CIDER 0.8.2 (Java 1.8.0_121, Clojure 1.8.0, nREPL 0.2.12)

Solution

  • Assuming that you have these definitions

    (def car first)
    (def cdr rest)
    

    The recursive call to map-edit only uses 3 arguments

    The line probably should be

    (map-edit (get m (car lst) {}) (cdr lst) k f))