Skip to content

Instantly share code, notes, and snippets.

@zaneli
Last active August 29, 2015 13:58
Show Gist options
  • Save zaneli/10008511 to your computer and use it in GitHub Desktop.
Save zaneli/10008511 to your computer and use it in GitHub Desktop.
「ClojureでNinety-Nine Lisp Problems(P01~05)」ブログ用
(defn my-last
"Find the last box of a list."
[xs]
(if (>= 1 (count xs))
xs
(my-last (rest xs))
)
)
(defn my-last
"Find the last box of a list."
[[x & xs]]
(if (empty? xs)
(if (nil? x) () (list x))
(my-last xs)
)
)
(defn my-but-last
"Find the last but one box of a list."
[xs]
(if (>= 2 (count xs))
xs
(my-but-last (rest xs))
)
)
(defn element-at
"Find the K'th element of a list."
[xs n]
(cond
(or (> 1 n) (empty? xs)) nil
(== 1 n) (first xs)
:else (element-at (rest xs) (dec n))
)
)
(defn element-at
"Find the K'th element of a list."
[[x & xs] n]
(cond
(or (> 1 n) (empty? xs)) nil
(== 1 n) x
:else (element-at xs (dec n))
)
)
(defn num-of-elem
"Find the number of elements of a list."
[xs]
((fn f [xs n]
(if (empty? xs)
n
(f (rest xs) (inc n))
)
) xs 0)
)
(defn num-of-elem
"Find the number of elements of a list."
[xs]
((fn [xs n]
(if (empty? xs)
n
(recur (rest xs) (inc n))
)
) xs 0)
)
(defn num-of-elem
"Find the number of elements of a list."
[xs]
(#(if (empty? %1)
%2
(recur (rest %1) (inc %2))
) xs 0)
)
(defn reverse-list
"Reverse a list."
[xs]
(#(if (empty? %1)
%2
(recur (rest %1) (cons (first %1) %2))
) xs '())
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment