Skip to content

Instantly share code, notes, and snippets.

@ujihisa
Created August 23, 2011 02:25

Revisions

  1. ujihisa created this gist Aug 23, 2011.
    54 changes: 54 additions & 0 deletions l99
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    (defn my-last [xs]
    (cond
    (empty? (rest xs)) (first xs)
    :otherwise (recur (rest xs))))
    (println
    (my-last '(a b c d)))

    (defn my-but-last [xs]
    (cond
    (empty? (rest (rest xs))) xs
    :otherwise (recur (rest xs))))
    (println
    (my-but-last '(a b c d)))

    (defn element-at [xs n]
    (cond
    (empty? xs) "failed"
    (= n 1) (first xs)
    :otherwise (recur (rest xs) (- n 1))))
    (println
    (element-at '(a b c d e) 3))

    (defn length [xs]
    (defn length2 [xs n]
    (cond
    (empty? xs) n
    :otherwise (recur (rest xs) (+ n 1))))
    (length2 xs 0))
    (println
    (length '(a b c d e)))

    (defn my-reverse [xs]
    (defn reverse2 [xs ys]
    (cond
    (empty? xs) ys
    ;:otherwise (recur (rest xs) (cons (first xs) ys))))
    :otherwise (recur (rest xs) (conj ys (first xs)))))
    (reverse2 xs '()))
    (println
    (my-reverse '(a b c d e)))

    (defn palindrome? [xs]
    (= xs (my-reverse xs)))
    (println
    (palindrome? '(a b c d e)))
    (println
    (palindrome? '(a b c b a)))

    (defn my-flatten [xs]
    (cond
    (list? (first xs))
    :otherwise 2))
    (println
    (my-flatten '((a b) c (d (e)))))