Skip to content

Instantly share code, notes, and snippets.

@sshark
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save sshark/00740f4298cca7f8c479 to your computer and use it in GitHub Desktop.

Select an option

Save sshark/00740f4298cca7f8c479 to your computer and use it in GitHub Desktop.
Solutions to some of the simple problems like BSF walking down a tree and TCO recursion.
(defn sumAll [x]
(if (= x 1)
1
(+ x (sumAll (dec x)))
))
(defn accSumAll [x]
((fn [y acc]
(if (zero? y)
acc
(recur (dec y) (+ acc y)))
) x 0))
(defn fibs [a b]
(lazy-seq (cons b (fibs b (+ a b)))))
(reduce #(vals %1) [] n)
(defn sumAll [x]
(if (empty? x)
0
(+ (first x) (sumAll (second x)))))
(def m [:a [
[:b [
[:e] [:f] [:g]]]
[:c [
[:h [
[:i [
[:j]]]]]]]
[:d [
[:k]]]]])
(defn path [root goal]
(letfn [(_path [currentNode queue]
(def node (first currentNode))
(def children (second currentNode))
(def history (nth currentNode 2))
(if (= node goal)
(conj history node )
(do
(def newQueue (into (map #(vector (first %1) (second %1) (conj history node)) children) queue))
(if (empty? newQueue)
nil
(do
(recur (first newQueue) (rest newQueue)))
)))
)]
(_path (vector (first root) (second root) []) [])))
(path m :j)
; [:a :c :h :i :j]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment