Last active
August 29, 2015 14:11
-
-
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.
This file contains hidden or 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
| (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