Skip to content

Instantly share code, notes, and snippets.

@ujihisa
Created August 23, 2011 02:25
Show Gist options
  • Save ujihisa/1164178 to your computer and use it in GitHub Desktop.
Save ujihisa/1164178 to your computer and use it in GitHub Desktop.
(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)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment