apt-get update
apt-get install -y zlib1g-dev libncurses5-dev ghc cabal-install happy alex
cabal update
cabal install cabal-install
export PATH=$HOME/.cabal/bin:./.cabal-sandbox/bin:$PATH
;;Code rewriting macro | |
(defmacro loop-with-timeout [timeout bindings & forms] | |
`(let [starttime# (System/currentTimeMillis)] | |
(loop ~bindings | |
(if (> (- (System/currentTimeMillis) starttime#) ~timeout) | |
(throw (RuntimeException. (str "Hit timeout of " ~timeout "ms."))) | |
(do ~@forms))))) | |
;;example use of macro | |
(loop-with-timeout 60000 [] |
(ns propagator.spreadsheet ; see http://gitweb.adaptive.cs.unm.edu/propagator.git | |
(use propagator)) | |
(def xrange 10) (def yrange 10) | |
(def spreadsheet | |
(map (fn [_] (map (fn [_] | |
(let [c (gensym)] (eval `(defcell ~c nil)) c)) | |
(range xrange))) | |
(range yrange))) |
(use ' clojure.contrib.reflect) | |
(defn- gf [obj field] | |
[(keyword (.getName field)) (get-field (class obj) (.getName field) obj)]) | |
(defn get-all-fields [obj] | |
(into (sorted-map) (map (partial gf obj) (.getDeclaredFields (class obj))))) | |
;; user=> (def a (memoize (partial (comp inc last concat) [2]))) | |
;; #'user/a |
(ns test) | |
(defn try-times | |
"Try executing a thunk `retries` times." | |
[retries thunk] | |
(if-let [res (try | |
[(thunk)] | |
(catch Exception e ; can be any exception | |
(when (zero? retries) | |
(throw e))))] |
(ns dcg | |
(:refer-clojure :exclude [reify == inc]) | |
(:use (clojure.core.logic minikanren prelude))) | |
(declare sentence noun-phrase verb-phrase det noun verb) | |
(defn sentence [s1 s3] | |
(exist [s2] | |
(noun-phrase s1 s2) | |
(verb-phrase s2 s3))) |
(defne geto [k m v] | |
([_ [[k :- v] . _] _]) | |
([_ [_ . ?r] _] (geto k ?r v))) | |
(defn typedo [c x t] | |
(conde | |
((geto x c t)) | |
((matche [c x t] | |
([_ [:apply ?a ?b] _] | |
(exist [s nc] |
(def-->e verb [v] | |
([[:v 'eats]] '[eats])) | |
(def-->e noun [n] | |
([[:n 'bat]] '[bat]) | |
([[:n 'cat]] '[cat])) | |
(def-->e det [d] | |
([[:d 'the]] '[the]) | |
([[:d 'a]] '[a])) |
(defne assoco [m k v o] | |
([[] _ _ [[k v]]]) | |
([[[k v] . _] _ _ m]) | |
([[[k ?v] . ?r] _ _ [[k v] . ?r]] | |
(!= m o)) | |
([[[?j v]] _ _ [[?j v] [k v]]] | |
(!= ?j k)) | |
([[[?j ?u] . ?r] _ _ [[?j ?u] . ?o]] | |
(!= ?j k) | |
(!= m o) |
;; Evolving a logic programming language | |
;; Based on sketches at https://github.com/frenchy64/Logic-Starter/blob/master/src/logic_introduction/decl_model.clj | |
;; A logic statement reduces to true or false. | |
true | |
;=> true | |
false | |
;=> false |