Skip to content

Instantly share code, notes, and snippets.

@orb orb/once.clj
Created Feb 28, 2014

Embed
What would you like to do?
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
You can’t perform that action at this time.