Skip to content

Instantly share code, notes, and snippets.

@kitallis
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitallis/dbe1c9bd7c997637f70e to your computer and use it in GitHub Desktop.
Save kitallis/dbe1c9bd7c997637f70e to your computer and use it in GitHub Desktop.
4clojure solutions
;; https://www.4clojure.com/problem/19
(fn [x] (first (reverse x)))
;; https://www.4clojure.com/problem/20
(fn [x] (second (reverse x)))
(fn [x] (first (rest (reverse x))))
;; https://www.4clojure.com/problem/21
(fn nth-element [c, n] (second (take-nth n s)))
(fn nth-element [c n] (if (zero? n) (first coll)
(nth-element (rest coll) (- n 1))))
(fn [c n] (if (zero? n) (first c)
(recur (rest c) (- n 1))))
(fn [c n] (if (zero? n) (first c)
(recur (rest c) (dec 1))))
(fn [c n] ((apply comp (cons first (repeat n rest))) s))
;; https://www.4clojure.com/problem/22
(fn [c] (reduce (fn [n e] (inc n)) 0 c))
(fn [c] (reduce + (map (fn [x] 1) c)))
;; https://www.4clojure.com/problem/23
(fn [c] (reduce (fn [a i] (cons i a)) '() c))
;; https://www.4clojure.com/problem/24
(fn [c] (reduce + c))
;; https://www.4clojure.com/problem/25
(fn [x] (take x ((fn fib [a b] (cons a (lazy-seq (fib b (+ a b))))) 1 1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment