2014-01-26 00:04:22 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "Lists can be expressed by function or a quoted form"
|
2016-02-27 19:34:11 +00:00
|
|
|
(= '(1 2 3 4 5) (list 1 2 3 4 5))
|
2010-02-20 15:30:24 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "They are Clojure seqs (sequences), so they allow access to the first"
|
2016-02-27 19:34:11 +00:00
|
|
|
(= 1 (first '(1 2 3 4 5)))
|
2010-02-20 15:30:24 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "As well as the rest"
|
2016-02-27 19:34:11 +00:00
|
|
|
(= 2 3 4 5 (rest '(1 2 3 4 5))) ;; J -
|
2010-02-20 15:30:24 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "Count your blessings"
|
2016-02-27 19:34:11 +00:00
|
|
|
(= 3 (count '(dracula dooku chocula)))
|
2013-03-04 22:58:58 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "Before they are gone"
|
2016-02-27 19:34:11 +00:00
|
|
|
(= 0 (count '()))
|
2013-03-04 22:58:58 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "The rest, when nothing is left, is empty"
|
2016-02-27 19:34:11 +00:00
|
|
|
(= (rest '(100))) ;; J - returns nothing.
|
2010-02-20 15:30:24 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "Construction by adding an element to the front is easy"
|
2016-02-27 19:34:11 +00:00
|
|
|
(= :a :b :c :d :e (cons :a '(:b :c :d :e))) ;; J - cons add's the element on the left to the beginning
|
|
|
|
|
;; of the list on the right and returns a seq.
|
|
|
|
|
;;(EX: (cons [1 2] '(3 4 5)) => '([1 2] 3 4 5)
|
2010-02-20 15:30:24 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "Conjoining an element to a list isn't hard either"
|
2016-02-27 19:34:11 +00:00
|
|
|
(= :a :b :c :d :e (conj '(:a :b :c :d) :e))
|
|
|
|
|
;; J - conj works like cons, but takes any number of arguments from
|
|
|
|
|
;; the right and adds them to the beginning of the list on the left
|
|
|
|
|
;; returning them in the structure of whatever the list on the left is defined
|
|
|
|
|
;; as originally. (EX: (conj [4 5 6] 1 2 3) => [1 2 3 4 5 6]
|
2010-02-20 15:30:24 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "You can use a list like a stack to get the first element"
|
2016-02-27 19:34:11 +00:00
|
|
|
(= :a (peek '(:a :b :c :d :e))) ;; J - returns the first element
|
2010-02-20 15:30:24 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "Or the others"
|
2016-02-27 19:34:11 +00:00
|
|
|
(= (:b) (pop '(:a :b :c :d :e))) ;; J - pops the first element and returns the others as a seq
|
2010-02-20 15:30:24 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "But watch out if you try to pop nothing"
|
2016-02-27 19:34:11 +00:00
|
|
|
(= (try ;; J - Catch raises an error similar to 'raise' in Ruby.
|
2010-02-20 15:30:24 +00:00
|
|
|
(pop '())
|
2013-03-04 22:58:58 +00:00
|
|
|
(catch IllegalStateException e
|
|
|
|
|
"No dice!")))
|
2010-02-20 15:30:24 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "The rest of nothing isn't so strict"
|
2016-02-27 19:34:11 +00:00
|
|
|
(= () (try
|
2010-02-20 15:30:24 +00:00
|
|
|
(rest '())
|
2013-03-04 22:58:58 +00:00
|
|
|
(catch IllegalStateException e
|
2016-02-27 02:33:05 +00:00
|
|
|
"No dice!")))
|