Skip to content

Instantly share code, notes, and snippets.

@orb
Created February 28, 2014 15:39
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/9273196 to your computer and use it in GitHub Desktop.
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
(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