Skip to content

Instantly share code, notes, and snippets.

View kpmaynard's full-sized avatar

Keith Maynard kpmaynard

View GitHub Profile
( defn fib [n]
(def memo (ref {})) ;create a ref to memo
(for [k (range (inc n))]
(do
(if (> k 1)
(dosync (alter memo conj [k (+ (@memo (- k 1)) (@memo (- k 2)))]) ;update memo with new pair
)
@kpmaynard
kpmaynard / fibo
Created May 18, 2014 01:50
bottom up fibonacci
(defn fib [n]
(def memo {0 0 1 1})
(for [k (range (inc n))]
(when (> k 1)
(assoc memo k (+ (memo (- k 1)) (memo (- k 2)))))
)
(memo n)
)
(let [origin (java.awt.Point. 0 0)]
(set! (.x origin) 15) ;; Assert failed: set! target must be a field or symbl naming a var targetexpr...
(str origin))
(let [origin (java.awt.Point. 0 0)]
(set! (.-x origin) 15) ;; this works dnolen example in another problem resolution
(str origin))
@kpmaynard
kpmaynard / gist:8611605
Created January 25, 2014 03:55
I can't figure out what I'm doing wrong here. (compute-winnings-for-a-trial ) returns a number, I am using conj to collect the outcomes of each trial for later analysis. I am not receiving any output whatever.
(defn multiple-trials [m n]
( loop [m m n n acc []]
(if (> m 0)
(conj acc (compute-winnings-for-a-trial n))
acc)
(recur (dec m) n acc)))
@kpmaynard
kpmaynard / gist:8479693
Created January 17, 2014 19:22
;; Unlike JavaScript loop locals are not mutable! In JavaScript you would see ;; a list of ten 9's. In ClojureScript we see the expected numbers from 0 to 9.
(let [fns (loop [i 0 ret []]
(if (< i 10)
(recur (inc i) (conj ret (fn [] i)))
ret))]
(map #(%) fns))
@kpmaynard
kpmaynard / gist:8367053
Created January 11, 2014 04:29
Permutations. Specification for permutations. Recursive formula. Let P represent Permutations of a sequence (a1, a2, a3, ... an) P()=() P(a1) = (a1) P(a1 a2) = ((a2 a1) (a1 a2)) <= (map #(weave a2 %) P(a1)) P(a1 a2 a3) = ((a3 a2 a1) (a2 a3 a1) (a2 a1 a3) (a3 a1 a2) (a1 a3 a2) ( a1 a2 a3))... => (map #(weave a3 %) P(a1 a2)) Assume P(a1, a2,.... a…
(defn weave
([a-seq]
(weave (first a-seq) (rest a-seq)))
([x a-seq]
(defn weave-helper [x a-seq acc n]
(println "In weave: Sequence is: " a-seq)
(cond (nil? a-seq) (cons x '())