yeh
yeh

Reputation: 1506

How to break a list in Clojure?

I am doing this exercise.

Here's my solution:

(defn infixcal [left op right & expr]
  (let [r (op left right)]
    (if (nil? expr)
      r
    (infixcal r expr))))

When i pass expression 38 + 48 - 2 / 2, I get ArityException because expr is gathered in a list '(- 2 / 2).

The question is how to break it into several arguments and pass it into the next call of function infixcal.

Upvotes: 1

Views: 218

Answers (1)

svk
svk

Reputation: 5919

I believe you want the function apply: http://clojuredocs.org/clojure_core/clojure.core/apply

This function takes another function f as its first argument and a list l as its second. The function f is then applied to the list of arguments (and the return value is returned from apply).

Upvotes: 1

Related Questions