Created
June 30, 2010 21:56
-
-
Save rcampbell/459278 to your computer and use it in GitHub Desktop.
Ninety-Nine Clojure Problems
This file contains 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
;; http://aperiodic.net/phil/scala/s-99/ | |
; last | |
(defn p01 [[x & xs]] | |
(if xs (recur xs) x)) | |
(defn p02 [xs] | |
(p01 (butlast xs))) | |
; nth | |
(defn p03 [n [x & xs]] | |
(if (zero? n) x (recur (dec n) xs))) | |
; count | |
(defn p04 [[x & xs]] | |
(if x (inc (p04 xs)) 0)) | |
; reverse | |
(defn p05 [[x & xs]] | |
(if x (concat (p05 xs) (list x)) (list))) | |
(defn p06 [xs] | |
(= (p05 xs) xs)) | |
; flatten | |
(defn p07 [[x & xs]] | |
(cond (nil? x) (list) | |
(coll? x) (concat (p07 x) (p07 xs)) | |
:else (cons x (p07 xs)))) | |
(defn p08 [[x & xs]] | |
(cond (nil? x) (list) | |
(= x (first xs)) (p08 xs) | |
:else (cons x (p08 xs)))) | |
;; FULLLL STOP | |
(defn p14 [[x & xs]] | |
(if x (cons x (cons x (p14 xs))) (list))) | |
(defn p15 [n [x & xs]] | |
(if x (concat (repeat n x) (p15 n xs)) (list))) | |
(defn p16 [n xs] | |
(mapcat butlast (partition n xs))) | |
; split-at | |
(defn p17 [n xs] | |
(list (take n xs) (drop n xs))) | |
(defn p20 [n [x & xs]] | |
(cond (nil? x) (list) | |
(zero? n) (p20 (dec n) xs) | |
:else (cons x (p20 (dec n) xs)))) | |
(defn p21 [e n [x & xs]] | |
(cond (nil? x) (list) | |
(zero? n) (cons e (cons x (p21 e (dec n) xs))) | |
:else (cons x (p21 e (dec n) xs)))) | |
; range | |
(defn p22 [from to] | |
(if (= from to) (list to) | |
(cons from (p22 (inc from) to)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment