Skip to content

Instantly share code, notes, and snippets.

@gabehollombe gabehollombe/gist:7854417
Last active Dec 30, 2015

Embed
What would you like to do?
Clojure from the Ground Up - Sequences - Exercises My answers to the exercises from: http://aphyr.com/posts/304-clojure-from-the-ground-up-sequences
; Sequences
;--------------------------------------------------
; Write a function to find out if a string is a palindrome–that is, if it looks the same forwards and backwards.
(defn revStr
"Reverses s and returns as a string"
[s]
(apply str (reverse s)))
(defn palindrome?
[s]
(= s (revStr s)))
; Find the number of ‘c’s in “abracadabra”.
(count (filter #(= % \c) "abracadabra"))
; Or
((frequencies "abracadabra") \c)
; Write your own version of filter.
(defn my-filter
[pred coll]
(reduce (fn [result val]
(if (pred val)
(conj result val)
result))
[]
coll))
; Find the first 100 prime numbers
(defn divisible? [x y] (= (rem x y) 0))
(defn prime? [n]
(not-any? #(divisible? n %) (range 2 n)))
(take 100 (filter prime? (iterate inc 2) ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.