This file contains 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
(defstruct game :current-score :throws) | |
(def darts | |
(concat | |
(range 1 21) | |
(map (partial * 2) (range 1 21)) | |
(map (partial * 3) (range 1 21)) | |
'(25 50))) | |
(def finishes |
This file contains 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 solve-darts-depth-first | |
[n] | |
(depth-first-search | |
(struct game n []) | |
finished? | |
next-dart)) | |
(defn solve-darts-breadth-first | |
[n] | |
(breadth-first-search (struct game n []) finished? next-dart)) |
This file contains 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 solve-darts-beam-search | |
[n] | |
(beam-search | |
(struct game n []) | |
finished? | |
next-dart | |
(fn [d] (/ (- (:current-score d) n) (count (:throws d)))) | |
3)) |
This file contains 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 new-states | |
"Generate successor states that have not been seen before." | |
[states successors state-eq old-states] | |
(remove | |
(fn [state] | |
(or (some (partial state-eq state) old-states) | |
(some (partial state-eq state) states))) | |
(successors (first states)))) | |
(defn graph-search |
This file contains 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
(defstruct path :state :previous :cost-so-far :total-cost) | |
(defn path-to-string | |
[path] | |
(format "Path to %s, cost %s" (:state path) (:total-cost path))) | |
(defn make-path | |
"Create a new path object" | |
[state previous cost-so-far total-cost] | |
(struct path state previous cost-so-far total-cost)) |
This file contains 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
(def grid-size 30) | |
;; A collection of walls | |
(def walls (atom #{})) | |
;; The route to the finish | |
(def route (atom #{})) | |
(defstruct point :x :y) |
This file contains 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 join-all | |
"Join the lists provided using f to select an element each time" | |
[f & lists] | |
(let [l (remove empty? lists)] | |
(when-not (empty? l) | |
(let [n (reduce f (map first l)) | |
c (count (filter (partial = true) (map (fn [x] (= (first x) n)) l))) | |
r (map (fn [x] (if (= (first x) n) (rest x) x)) l)] | |
(lazy-seq | |
(concat |
This file contains 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 get-entries-from-rss | |
"Get a list of entries from an RSS feed" | |
[src] | |
(xm/xml-> (zip/xml-zip (parse-trim src)) :entry)) | |
(defn get-date | |
"Given an RSS entry, get the date" | |
[entry] | |
(xm/xml-> entry :published xm/text)) |
This file contains 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 rss-merge | |
"Join together the two feeds specified, using min-date as the selector which grabs the minimum | |
date from the published field" | |
[feed1 feed2] | |
(join-all | |
min-date | |
(get-entries-from-rss feed1) | |
(get-entries-from-rss feed2))) |
This file contains 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
(defvar | |
dispatch-table | |
{:single-match | |
{'?is match-is '?or match-or '?and match-and '?not match-not} | |
:segment-match | |
{'?* segment-match '?+ segment-match+ '?? segment-match? '?if match-if}} | |
"Dispatch table") | |
(defn single-match-fn | |
"Get the single-match function for x, if it is a symbol that has one." |