"wrong number of args passed to keyword"


#1

Just getting started, in “Intro to Clojure”, Exercise 1.

I tried to create a function for adding milk/sugar/flour. It worked fine, but I forgot to release the cup. When I added that to the function, I got the following error:

user=> (defn add-scoop [ n stuff ]
  #_=> (dotimes [m n] ((grab :cup) (scoop stuff) (add-to-bowl) (release))))
#'user/add-scoop
user=> (add-scoop 1 :flour)
IllegalArgumentException Wrong number of args passed to keyword: :error  clojure.lang.Keyword.throwArity (Keyword.java:97)

I see in the REPL that executing one function at time returns :ok. So I guess I have inadvertently set up a situation where :ok is being used as function … not sure why that blows up when I have four forms in my function, but not when I had three.

I’m sure this is a basic mistake. Appreciate any help!


#2

Hey Brent,

Thanks for the question.

The problem is that you are wrapping things in one extra set of parentheses. The reason it’s using :ok as a function is because of those parens. Remember: parens mean function call. They’re not for grouping like in most languages.

Here’s what it should look like. (Note I’ve formatted it to make it easier to see the structure.)

(defn add-scoop [ n stuff ]
  (dotimes [m n] 
    (grab :cup)
    (scoop stuff) 
    (add-to-bowl) 
    (release)))

It was working before because keywords can act as functions that take one or two parameters. When you added the third, it broke.

Rock on!
Eric


#3

Aha! Thanks, Eric. I will have to watch my parens, definitely a habit to change.


#4

Eric, can we create categories? If not, I think there should be a category for “Mentoring Help - Questions”.