Last active
August 29, 2015 13:58
-
-
Save zaneli/10008511 to your computer and use it in GitHub Desktop.
「ClojureでNinety-Nine Lisp Problems(P01~05)」ブログ用
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn my-last | |
"Find the last box of a list." | |
[xs] | |
(if (>= 1 (count xs)) | |
xs | |
(my-last (rest xs)) | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn my-last | |
"Find the last box of a list." | |
[[x & xs]] | |
(if (empty? xs) | |
(if (nil? x) () (list x)) | |
(my-last xs) | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn my-but-last | |
"Find the last but one box of a list." | |
[xs] | |
(if (>= 2 (count xs)) | |
xs | |
(my-but-last (rest xs)) | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn num-of-elem | |
"Find the number of elements of a list." | |
[xs] | |
(#(if (empty? %1) | |
%2 | |
(recur (rest %1) (inc %2)) | |
) xs 0) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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