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
(ns cljpuzzle.core | |
(:use [clojure.math.combinatorics])) | |
(defn solve-logic-puzzle [] | |
(let [people [:amaya :bailey :jamari :jason :landon]] | |
(first | |
(for [[fortune time cosmopolitan us-weekly vogue] (permutations people) ; magazines | |
[asiago blue-cheese mascarpone mozzarella muenster] (permutations people) ; cheeses | |
; We bind the reservations in two steps, so we have a name for the overall order | |
reservations (permutations people) |
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
(use '[clojure.core.match :only [match]]) | |
(defn evaluate [env [sym x y]] | |
(match [sym] | |
['Number] x | |
['Add] (+ (evaluate env x) (evaluate env y)) | |
['Multiply] (* (evaluate env x) (evaluate env y)) | |
['Variable] (env x))) | |
(def environment {"a" 3, "b" 4, "c" 5}) |
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 re? | |
([p] | |
(fn [s] (re-find (re-pattern p) (str s)))) | |
([p s] ((re? p) s))) | |
(defn mapentry-matches? [pat [k v]] | |
(or (re? pat (-> k str string/lower-case)) | |
(re? pat (-> v str string/lower-case)))) | |
;; filter keys and values in json-like nested maps and vectors |
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 pool (java.util.concurrent.Executors/newFixedThreadPool 2)) | |
(def l1 (java.util.concurrent.CountDownLatch. 1)) | |
(def l2 (java.util.concurrent.CountDownLatch. 1)) | |
(defn worker [] (.await l1) (println "l1") (.await l2) (println "done!")) | |
(dotimes [_ 4] (.submit pool worker)) | |
(.countDown l1) | |
;; l1 | |
;; l1 |
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
(use '[clojure.core.match :only [match]]) | |
(defn evaluate [env [sym x y]] | |
(match [sym] | |
['Number] x | |
['Add] (+ (evaluate env x) (evaluate env y)) | |
['Multiply] (* (evaluate env x) (evaluate env y)) | |
['Variable] (env x))) | |
(def environment {"a" 3, "b" 4, "c" 5}) |
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 sqrtStream(x: Double): Stream[Double] = { | |
def improve(guess: Double) = (guess + x / guess) / 2 | |
lazy val guesses: Stream[Double] = 1 #:: (guesses map improve) | |
guesses | |
} | |
sqrtStream(4).take(10).toList |
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
import scala.io.Source | |
object mnem { | |
val in = Source.fromURL("http://lamp.epfl.ch/files/content/sites/lamp/files/teaching/progfun/linuxwords.txt") | |
val words = in.getLines.toList filter (word => word forall (c => c.isLetter)) | |
val mnem = Map( | |
'2' -> "ABC", '3' -> "DEF", 4 -> "GHI", '5' -> "JKL", | |
'6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", 9 -> "WXYZ") |
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
(defne not-membero [x l] | |
([_ []]) | |
([_ [h . t]] | |
(!= x h) | |
(not-membero x t))) | |
(defne subseto [s1 s2] | |
([() _]) | |
([[x . xs] _] | |
(membero x s2) |
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
(ns cl-graph | |
(:refer-clojure :exclude [==]) | |
(:use clojure.core.logic)) | |
;; Directed Acyclic Graphs | |
(defrel edge a b) | |
;; a | |
;; | |
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
(ns graph-cl | |
(:refer-clojure :exclude [==]) | |
(:use clojure.core.logic)) | |
(defrel edge a b) | |
(fact edge :b :a) | |
(fact edge :c :a) | |
(fact edge :d :b) | |
(fact edge :d :c) |