butlast

added
1.0

ns
clojure.core

type
function

(butlast coll)

Return a seq of all but the last item in coll, in linear time

                user=> (butlast [1 2 3])
(1 2)
user=> (butlast (butlast [1 2 3]))
(1)
user=> (butlast (butlast (butlast [1 2 3])))
nil
            
                ;really slow reverse
;put the last item of the list at the start of a new list, and recur over all but the last item of the list.
;butlast acts similar to next in that it returns null for a 1-item list.

(defn my-reverse [xs]
  (when xs
    (cons (last xs) (my-reverse (butlast xs)))))