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
;; adapted from the Sudoku solver in `The Joy of Clojure 2nd Edition' 2014 by Michal Fogus and Chris Houser, Chater 16 Thinking Programs | |
(require '[clojure.core.logic :as l] ;;the logic engine | |
'[clojure.core.logic.fd :as fd]) ;;dealing with finite domains (integer numbers) | |
(defn latin-squares [n num-of-sols] ;; n - size of the square, num-fo-sols - number of solutions | |
(let [tab (vec (repeatedly (* n n) l/lvar)) ;; n by n table of logic variables | |
rows (partition n tab) ;; rows of the table | |
cols (apply map vector rows) ;; columns of the table | |
points (fd/interval 0 (dec n))] ;; valid entries | |
(l/run num-of-sols [q] |
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
;; Caesar shift and its brute-force attack. | |
(def letters "abcdefghijklmnopqrstuvwxyz ") | |
(defn shift-map | |
"A hash-map that maps letters to letters cyclically shifted by n." | |
[n] | |
(zipmap letters | |
(take (count letters) | |
(drop n (cycle letters))))) |
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
;; simple implementation of the Burrows-Wheeler transform (used in bzip2) | |
;; https://en.wikipedia.org/wiki/Burrows-Wheeler_transform | |
(defn rotations | |
"All rotations (cyclic permutations) of a string s." | |
[s] | |
(let [n (count s)] | |
(reduce (fn [rotations i] | |
(conj rotations | |
(apply str |
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
; representation of a line defined by two points | |
(def l [[1 2] [2 4]]) | |
;information extracted on demand, actual calculation obscure | |
(defn slope | |
[line] | |
(/ | |
(- (first (first line)) (first (second line))) | |
(- (second (first line)) (second (second line))))) |
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 merge-sort | |
"sorting the given collection with merge-sort" | |
[coll] | |
(if (or (empty? coll) (= 1 (count coll))) | |
coll | |
(let [[l1 l2] (split-at (/ (count coll) 2) coll)] | |
;recursive call | |
(loop [r [] l1 (merge-sort l1) l2 (merge-sort l2)] | |
;merging | |
(cond (empty? l1) (into r l2) ;when l1 is exhausted |