Skip to content

Instantly share code, notes, and snippets.

@lynaghk
Created June 2, 2013 18:06
Show Gist options
  • Save lynaghk/5694320 to your computer and use it in GitHub Desktop.
Save lynaghk/5694320 to your computer and use it in GitHub Desktop.
Puzzle agent + core.logic.
(defn sublisto
"The sequence x appears in y."
[x y]
(fresh [a b c]
(appendo a x b)
(appendo b c y)))
(->> (run* [q]
(sublisto [:b :r :r :c] q)
(sublisto [:r :c :c :b] q)
(sublisto [:c :r :c :c] q)
(sublisto [:r :c :c :r] q)
(sublisto [:c :r :c :r :c] q)
;;(pred q #(< (count %) 16))
;;(everyg #(membero % [:b :r :c]) q)
)
(take 4)
pprint)
;;I need to get:
;;[b r r c c r c r c r c c b]
@swannodette
Copy link

I think this is closer to what you want:

(defne sublisto [x y]
  ([() _])
  ([[h . xt] [h . yt]]
    (sublisto xt yt))
  ([[h . _] [i . yt]]
    (!= h i)
    (sublisto x yt)))

@lynaghk
Copy link
Author

lynaghk commented Jun 2, 2013

This is strange:

(defne prefixo [x y]
  ([() _])
  ([[h . xt] [h . yt]]
     (prefixo xt yt)))

(defne sublisto [x y]
  ([() _])
  ([[h . xt] [h . yt]]
     (prefixo xt yt))
  ([[h . _] [i . yt]]
     (!= h i)
     (sublisto x yt)))

(run* [q]
     (prefixo [:b :r :r :c] q)
     (sublisto [:b :r :r :c] q) 
     (sublisto [:r :c :c :b] q))
;;=> ()

@swannodette
Copy link

This works

(defne gen-listo [l]
  ([()])
  ([[h . t]]
    (gen-listo t)))

(defne prefixo [x y]
  ([() ()])
  ([() [h . t]])
  ([[xh . xt] [xh . yt]]
    (prefixo xt yt)))

(defne sublisto [x y]
  ([[xh . xt] [xh . yt]]
    (prefixo xt yt))
  ([_ [_ . yt]]
    (sublisto x yt)))

(comment
  (run 1 [q]
    (gen-listo q)
    (sublisto [:b :r :r :c] q)
    (sublisto [:r :c :c :b] q)
    (sublisto [:c :r :c :c] q)
    (sublisto [:r :c :c :r] q)
    (sublisto [:c :r :c :r :c] q))
  )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment