Skip to content

Instantly share code, notes, and snippets.

@mkuklis
Last active December 11, 2015 08:58
Show Gist options
  • Save mkuklis/4576884 to your computer and use it in GitHub Desktop.
Save mkuklis/4576884 to your computer and use it in GitHub Desktop.
playing with clojure
(def my-apply
(fn [function sequence]
(eval (cons function sequence))))
;; exercises
;; chapter 1
;; 3. add squares
(defn add-squares [& args]
(apply + (map * args args)))
;; 4
(defn fact [num]
(apply * (range 1 (inc num))))
;; 5
(take 2 [1 2 3 4]) => (1 2)
(drop 2 [1 2 3 4]) => (3 4)
(drop-last 2 [1 2 3 4]) => (1 2)
(distinct [2 2 3]) => (2 3)
(concat [2] [3]) => (2 3)
(interleave [1 2] [3 4]) => (1 3 2 4)
(partition 2 [1 2 3 4]) => ((1 2) (3 4))
(every? (fn [item] (number? item)) [1 2 3]) => true
(remove true? [true false false]) => (false false)
;; 6
(defn prefix-of? [candidate sequence]
(= candidate (take (count candidate) sequence)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment