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 dfs [g] | |
(let [visited (atom #{})] | |
(letfn | |
[(search_tree [v] | |
(if (not (@visited v)) | |
(do | |
(swap! visited conj v) | |
(cons v (search (g v)))))) | |
(search [vs] | |
(if (seq vs) |
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 pattern-graph | |
[[0 1 0 1 1 1 0 1 0] | |
[1 0 1 1 1 1 1 0 1] | |
[0 1 0 1 1 1 0 1 0] | |
[1 1 1 0 1 0 1 1 1] | |
[1 1 1 1 0 1 1 1 1] | |
[1 1 1 0 1 0 1 1 1] | |
[0 1 0 1 1 1 0 1 0] | |
[1 0 1 1 1 1 1 0 1] | |
[0 1 0 1 1 1 0 1 0]]) |
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 mult? | |
"Returns true iff n is a multiple of one of the numbers in coll." | |
[coll n] | |
(some #(zero? (rem n %)) coll)) | |
(def primes | |
(letfn [(rest-primes | |
[known-primes] | |
(lazy-seq | |
(let [highest-known-prime (peek known-primes), |
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 find-path | |
[graph from to] | |
((fn this [graph from to path] | |
(if (= from to) | |
(println "Found it:" path) | |
(let [children (for [v (graph from) :when (not (some #{v} path))] v)] | |
(if (empty? children) | |
(println "Dead end:" path) | |
(doseq [v children] | |
(this graph v to (conj path v))))))) |
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 deinterleave | |
[coll n] | |
(->> | |
(loop [out [(take n coll)] | |
tail (drop n coll)] | |
(if (seq tail) | |
(recur (conj out (take n tail)) (drop n tail)) | |
out)) | |
(apply map vector))) |
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
(defprotocol Graph | |
(set-vertex [this v]) | |
(set-edge [this from to] | |
[this from to weight])) | |
(defrecord DirectedGraph [vs em wm] | |
;; A directed graph: vs is a sorted set of vertices, em is a map from vertices | |
;; to a sorted set of adjacent vertices, wm is a map from edges given as | |
;; vertex tuple [from to] to weights. | |
Graph |
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
(defrecord DirectedGraph [vertices edges weights] | |
;; A directed graph: 'vertices' is a sorted set of vertices, 'edges' is a map | |
;; from vertices to a sorted set of adjacent vertices, 'weights' is a map from | |
;; edges given as vertex tuple [from to] to weights. | |
Graph | |
(set-vertex [_ v] | |
(DirectedGraph. (conj vertices v) | |
(conj edges [v (sorted-set)]) | |
weights)) | |
(set-edge [this edge] |
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 gen-prefn | |
"Generates a function that for input q returns the lengths of the longest | |
prefix of pattern that is also a suffix of pattern[1..q]." | |
[pattern] | |
(let [pattern (vec pattern) | |
add-entry (fn [[pfn l] q] | |
(if (= (pattern l) (pattern q)) | |
[(conj pfn [(inc q) (inc l)]) (inc l)] | |
(if (zero? l) | |
[(conj pfn [(inc q) 0]) 0] |
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
unreadable message: (:emacs-rex (swank:listener-eval "(println \"â\") | |
") "user" :repl-thread 6 | |
exception in read loop | |
java.lang.RuntimeException: java.lang.Exception: EOF while reading | |
at clojure.lang.RT.readString(RT.java:1263) | |
at clojure.core$read_string.invoke(core.clj:2897) | |
at swank.core.protocol$read_swank_message$fn__240.invoke(protocol.clj:44) | |
at swank.core.protocol$read_swank_message.invoke(protocol.clj:43) | |
at swank.core.connection$read_from_connection.invoke(connection.clj:59) | |
at swank.core$read_loop.invoke(core.clj:337) |
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 map- [f coll] | |
(lazy-seq (cons (f (first coll)) (map- f (rest coll))))) |
OlderNewer