Skip to content

Instantly share code, notes, and snippets.

@jawher
Created July 8, 2010 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jawher/467463 to your computer and use it in GitHub Desktop.
Save jawher/467463 to your computer and use it in GitHub Desktop.
(ns s99)
(defn last-99 "P01" [l]
(reduce #(do %2) l))
(last-99 '(1 3 5 6))
(defn penultimate "P02" [l]
(first (rest (reverse l))))
(penultimate '(1 3 5 6))
(defn nth-s99 "P03" [n l]
(if (= 0 n) (first l) (nth-s99 (- n 1) (rest l))))
(nth-s99 3 '(1 3 5 6 8 11))
(defn length "P04" [l]
(if (empty? l) 0 (+ 1 (length (rest l)))))
(length '(1 3 5 6))
(defn reverse-s99 "P05" [l]
(if (empty? l) nil (cons (last l) (reverse-s99 (butlast l)))))
(reverse-s99 '(1 3 5 6))
(defn palyndrome? "P06" [l]
(= l (reverse l)))
(palyndrome? '(1 3 5 6))
(palyndrome? '(1 3 5 3 1))
(palyndrome? '(1 3 5 5 3 1))
(defn flatten "P07" [l]
(reduce #(concat %1 (if (list? %2) (flatten %2) (list %2))) '() l))
(flatten '(1 3 (5 6) (8) (9 (11 12))))
(defn compress "P08" [l]
(reduce #(if (= (last %1) %2) %1 (concat %1 (list %2))) '() l))
(compress '(1 1 3 1 3 3 5 6 6 7))
(defn pack "P09" [l]
(reverse (reduce
#(if (= (first (first %1)) %2)
(cons (cons %2 (first %1)) (rest %1))
(cons (list %2) %1 ))
'()
l))
)
(pack '(1 1 3 1 3 3 5 6 6 7))
(defn encode "P10" [l]
(map #(list (first %) (count %)) (pack l)))
(encode '(1 1 3 1 3 3 3 3 5 6 6 6 7))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment