34

I could have sworn I had alength working previously, but I don't quite know what I am doing wrong right now:

user=> (alength '(1 2 3))
IllegalArgumentException No matching method found: alength  clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:79)
user=> (alength [1 2 3])
IllegalArgumentException No matching method found: alength  clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:79)
user=> (doc alength)
-------------------------
clojure.core/alength
([array])
  Returns the length of the Java array. Works on arrays of all
  types.
nil

What should I be doing to get the length of a list/array in Clojure?

5 Answers 5

54

Try using count:

(count '(1 2 3))
=> 3
(count [1 2 3])
=> 3
Sign up to request clarification or add additional context in comments.

Comments

38

As the docstring says, alength works on Java™ arrays, such as a String[] or Integer[], which is definitely an incompatible type with Clojure lists or vectors, for which you want to use count:

user=> (def x '(1 2 3))
#'user/x
user=> (def xa (to-array x))
#'user/xa
user=> (class x)
clojure.lang.PersistentList
user=> (class xa)
[Ljava.lang.Object;
user=> (alength xa)
3
user=> (alength x) 
java.lang.IllegalArgumentException: No matching method found: alength (NO_SOURCE_FILE:0)
user=> (count x)
3

[Ljava.lang.Object; is the weird way toString is defined to output for native Object arrays.

1 Comment

Note, that count does also work for arrays. alength is only needed in speed-critical code where you already know that you have an array and need direct access. count would be slower here, because it's more generic.
14

It should be count.

user=> (count '(1 2 3))
3

Comments

2

This may be overkill, but you could imitate Common LISP's length function like this:

(def length 
 (fn [lst]
  (loop [i lst cnt 0]
   (cond (empty? i) cnt
     :t (recur (rest i)(inc cnt))))))

Comments

2

You can do that using the recursive way:

(defn length
 [list]
 (if (empty? list) 0
  (+ 1 (length (rest list)))))

Hope it helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.