I’m trying to write several functions which each follow a similar kind of argument parsing.
In all cases the functions are unary functions.
However, there are three cases to consider. 1) the argument is not a list, 2) the argument is a singleton list, 3) the argument is a list with a head and non-empty tail.
I tried the following, but the compiler complains. Can't have 2 overloads with same arity
What’s the correct way to do this?
(defn f
([expr]
;; case 1
1
)
([[token & args]]
(if (nil? args)
;; case 2
2
;; case 3
3)))
Incidentally, if you haven’t watched Clojure for Lisp Programmers (and part 2) I highly recommend it. Even without CL experience I consider it one of my favorite talks to explain the reasoning behind Clojure’s design decisions and idioms. In it, Rich gives one of the best explanations anywhere of the seq idiom.