Exercise 03 Lists - MikeyYeahYeah/clojure-koans GitHub Wiki

Chapter 03 - Lists

Creating a List

  • (list 1 2 3 4 5)
  • '(1 2 3 4 5)

Functions to work on lists

First

(first '(1 2 3 4 5)
 	;;=> 1

Rest

(rest '(1 2 3 4 5))
	;;=> (2 3 4 5)
    
(rest '(100))
	;;=> nil

Count

(count '("Bob" "Alice" "Jane")
	;;=> 3

Construction

Returns a new seq where x is the first element and seq is the rest.

(cons :a '(:b :c :d :e))
	;;=> (:a :b :c :d :e)

Conjoining

Note: Conjoin strives to add a new item to the most efficient place possible for the collection type. In the case of a list, it will be added to the front.

(conj '(:b :c :d :e) :a)
	;;=> (:a :b :c :d :e)

Peek

For a list or queue, same as first, for a vector, same as, but much more efficient than, last. If the collection is empty, returns nil.

(peek '(:a :b :c :d :e))
  ;;=> :a

Pop

For a list or queue, returns a new list/queue without the first item

(pop '(:a :b :c :d :e))
  ;;=> (:b :c :d :e)