Skip to content

Instantly share code, notes, and snippets.

@neotyk
Created March 27, 2012 21:06
Show Gist options
  • Save neotyk/2220234 to your computer and use it in GitHub Desktop.
Save neotyk/2220234 to your computer and use it in GitHub Desktop.
right? and righto
(ns l1
(:use [clojure.core.logic
:only [fresh conde firsto resto run]]))
(defn right?
[a b l]
(or (and (== a (first l))
(== b (second l)))
(when-let [t (next l)]
(recur a b t))))
(comment
(right? 1 2 [1 2 3])
;;=> true
(right? 2 3 [1 2 3])
;;=> true
(right? 1 3 [1 2 3])
;;=> nil
)
(defn righto
[X Y L]
(conde [(fresh [t]
(firsto L X)
(resto L t)
(firsto t Y))]
[(fresh [t]
(resto L t)
(righto X Y t))]))
(comment
(run 1 [q]
(righto 1 2 [1 2 3]))
;;=> (_.0)
(run 1 [q]
(righto 2 3 [1 2 3]))
;;=> (_.0)
(run 1 [q]
(righto 1 3 [1 2 3]))
;;=> ()
;; and for free you have
(run 1 [q]
(righto 1 q [1 2 3]))
;;=> (2)
(run 1 [q]
(righto 2 3 [1 2 q]))
;;=> (3)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment