Skip to content

Instantly share code, notes, and snippets.

@fffej
Created July 7, 2009 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fffej/142305 to your computer and use it in GitHub Desktop.
Save fffej/142305 to your computer and use it in GitHub Desktop.
(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
"Find a state that statisfies goal?. Start with states and search
according to successors and combiner. Don't repeat same state twice"
([states goal? successors combiner]
(graph-search states goal? successors combiner = #{}))
([states goal? successors combiner old-states]
(graph-search states goal? successors combiner = old-states))
([states goal? successors combiner state-eq old-states]
(dbg :search "Search: %s" states)
(cond
(empty? states) nil
(goal? (first states)) (first states)
:else (recur
(combiner (new-states states successors state-eq old-states)
(rest states))
goal? successors combiner state-eq
(conj old-states (first states))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment