Skip to content

Instantly share code, notes, and snippets.

@swannodette
swannodette / gist:1329743
Created November 1, 2011 03:02
matches.scm
(define (matches n)
(run #f (q)
(fresh (a b c d s1 s2)
(domfd a b c d s1 s2 (range 1 n))
(all-differentfd `(,a ,b ,c ,d))
(== a 1)
(<=fd a b) (<=fd b c) (<=fd c d)
(plusfd a b s1) (plusfd s1 c s2) (plusfd s2 d n)
(checko `(,a ,b ,c ,d) () () n)
(== q `(,a ,b ,c ,d)))))
@swannodette
swannodette / gist:1329766
Created November 1, 2011 03:14
subchecko.scm
(define (subchecko w sl r o n)
(conde
((== sl ())
(fresh (a d)
(domfd a (range 1 n))
(conde
((conso a d r) (plusfd a 1 w)
(conso w r o))
((== r '()) (conso w r o)))))
((fresh (a b c ro0 ro1 nw nsl)
(ns challenge.weights
(:require [clojure.contrib.combinatorics :as c]))
(defn weigh? [i l r]
(let [l-weight (apply + i l)]
(some #(when (= l-weight (apply + %)) [l %]) (set (c/subsets r)))))
(defn diff [l1 l2]
(reduce (fn [l i]
(let [[b [f & r]] (split-with (complement #{i}) l)]
@unclebob
unclebob / commatize
Created February 7, 2012 17:44
A function to format a number with commas
(defn commatize [n]
(if (nil? n)
""
(let [s (str n)]
(apply str
(reverse (drop-last
(interleave
(reverse s)
(take (count s) (flatten (repeat [nil nil \,]))))))))))
@budu
budu / send_more_money.clj
Created April 12, 2012 05:03
Logic puzzle: send more money solution in Clojure core.logic, translated from cKanren paper. Takes forever to run!
(use 'clojure.core.logic
'clojure.core.logic.arithmetic)
(defne diffo
[l]
([[]])
([[x]])
([[x y . tail]]
(fresh [l0 l1]
(!= x y)
@pelle
pelle / accounts.clj
Created May 8, 2012 14:37
Using database functions in Datomic transactions and annotating transaction history
(use '[datomic.api :only [q db] :as d])
(def uri "datomic:mem://accounts")
;; create database
(d/create-database uri)
;; connect to database
(def conn (d/connect uri))
@jboner
jboner / latency.txt
Last active June 27, 2024 14:47
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@stuarthalloway
stuarthalloway / gist:3068749
Created July 8, 2012 00:29 — forked from cs224/gist:3066392
datomic-clojure-relational-algebra-2012-07-07
;; Datomic example code
;; Demonstrates using datalog with Clojure defrecords
(use '[datomic.api :only [q db] :as d])
;;; http://www.lshift.net/blog/2010/08/21/some-relational-algebra-with-datatypes-in-clojure-12
(defrecord Supplier [number name status city])
(defrecord Part [number name colour weight city])
(defrecord Shipment [supplier part quantity])
;; sample data
@martintrojer
martintrojer / queries.clj
Created July 16, 2012 12:16
Datomic queries in core.logic
(ns queries
(:refer-clojure :exclude [==])
(:use [clojure.core.logic])
(:use [datomic.api :only [q]]))
(defn query [rule xs]
(let [prule (prep rule)]
(map #(binding-map* prule (prep %)) xs)))
;; ---
@swannodette
swannodette / sud4-ckanren.clj
Created July 27, 2012 19:41 — forked from martintrojer/sud4-ckanren.clj
Sudoku core.logic
(defne all-distinctfd [l]
([()])
([[h . t]]
(distinctfd h)
(all-distinctfd t)))
(run 1 [q]
(fresh [a1 a2 a3 a4
b1 b2 b3 b4
c1 c2 c3 c4