I have been looking through core.logic tutorials and while I "get it" I 
haven't had the big epiphany yet. One thing that keeps nagging me is how to 
make a relation that isn't "fixed".

An example is https://github.com/swannodette/logic-tutorial in which there 
is defined some relations such as parent, child, son, daughter and 
granddaughter. As I see it these are all a fixed relationships in that they 
are a fixed distance from each other.

For me that leads to the question, how do we then define the relationship 
'descendant' (which would be the generalisation of child, grandchild etc)? 

Seems to me that this would involve a recursive definition, but I don't 
know how to make one like that. Any hints?

This is the relevant code from the excellent tutorial made by swannodette, 
that I linked above:

(ns logic-tutorial.tut1
  (:refer-clojure :exclude [==])
  (:use [clojure.core.logic])
  (:use [clojure.core.logic.pldb]))

(def rels
         (db
           [male 'Bob]
           [female 'Cindy]
           [parent 'John 'Cindy]
           [parent 'Jane 'Cindy]
           [parent 'John 'Bob]
           [parent 'Jane 'Bob]))

(defn child [x y]
  (parent y x))

(defn son [x y]
  (all
   (child x y)
   (male x)))

(defn daughter [x y]
  (all
   (child x y)
   (female x)))

(defn grandparent [x y]
  (fresh [z]
    (parent x z)
    (parent z y)))

(defn granddaughter [x y]
  (fresh [z]
    (daughter x z)
    (child z y)))

;; Running it

(with-db rels
    (run* [q]
          (child 'Bob q)))

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to