Created
February 28, 2014 15:39
-
-
Save orb/9273196 to your computer and use it in GitHub Desktop.
a core.logic query occurs-onceo that states that x occurs exactly one time in xs
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 logic.once | |
(:refer-clojure :exclude [==]) | |
(:use [clojure.core.logic]) | |
(:require [clojure.core.logic.fd :as fd])) | |
(defne occurs-noneo [x xs] | |
([x []]) | |
([x [y . ys]] | |
(!= y x) | |
(occurs-noneo x ys))) | |
(comment | |
;; my first attempt, but this allows the ys in the second match to be empty, | |
;; which produces extra results | |
(defne occurs-onceo [x xs] | |
([x [x]]) | |
([x [y . ys]] | |
(conde | |
[(== x y) (occurs-noneo x ys)] | |
[(!= x y) (occurs-onceo x ys)]))) | |
;; this version adds a test for that, but then I realized I didn't really need the first clause | |
;; at all | |
(defne occurs-onceo [x xs] | |
([x [x]]) | |
([x [y . ys]] | |
(!= ys []) | |
(conde | |
[(== x y) (occurs-noneo x ys)] | |
[(!= x y) (occurs-onceo x ys)])))) | |
;; states that x occurs exactly one time in xs | |
(defne occurs-onceo [x xs] | |
([x [y . ys]] | |
(conde | |
[(== x y) (occurs-noneo x ys)] | |
[(!= x y) (occurs-onceo x ys)]))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment