Skip to content

Instantly share code, notes, and snippets.

@orb
Last active December 17, 2015 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orb/5663864 to your computer and use it in GitHub Desktop.
Save orb/5663864 to your computer and use it in GitHub Desktop.
Skeptics Guide to the Universe - Episode 410 - WTN http://www.theskepticsguide.org/ a core.logic finite domains example
(ns dollars-and-cents
(:refer-clojure :exclude [==])
(:use [clojure.core.logic])
(:require [clojure.core.logic.fd :as fd]))
;; A bank teller made a mistake today. The teller switched the dollars
;; and cents when they cashed a check for Mrs. Jones, giving her
;; dollars instead of cents and cents instead of dollars.
;; After buying a newspaper for 5 cents, Mrs. Jones realized that she
;; had remaining exactly twice as much as the original check.
;; What was the amount of the original check?
(defn dollars-and-cents []
(run* [dollars cents]
(fd/in dollars cents (fd/interval 99))
(fresh [pennies-correct pennies-reversed]
(fd/in pennies-correct pennies-reversed (fd/interval 9999))
(fd/eq
(= pennies-correct (+ (* 100 dollars) cents)))
(fd/eq
(= pennies-reversed (+ (* 100 cents) dollars)))
(fd/eq
(= (* 2 pennies-correct) (- pennies-reversed 5))))))
(defn dollars-and-cents-shorter []
(run* [dollars cents]
(fd/in dollars cents (fd/interval 99))
(fd/eq (= (* 2 (+ (* 100 dollars) cents))
(- (+ (* 100 cents) dollars) 5)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment