prolog sans prolog

138 views
Skip to first unread message

fuzzy wozzy

unread,
Dec 28, 2017, 7:48:24 AM12/28/17
to Shen
consider the following geneaology,

mark has three children, mary, crystal, and sam,
mary has three children, frank, amber, and john,
amber and john do no have children,
frank has three children, jonathan, bob and robert,
bob has one child, maggie,
jonathan, robert, and Maggie do not have children,

crystal has two children, tom and sally,
tom has two children, tom jr. and david,
tom jr. and david do not have children.

sally has three children, erica, Jennifer, and keith,
jennifer has one child, kyle,
erica, keith, and kyle do not have children.

sam has one child, eric, and eric has one child, suzy,
suzy has no children.

the challenge was to write a shen list program in a declarative way
to find a grandparent-grandchild relation for some or all of the
members in the database.

this is typical of relational database example via geneaology in prolog,
but shen list can do it too, superbly so, including (findall), which is an
icing on the cake, (findall) can find grandparent-grandchild relation for all
applicable members in the database, or any random point on the geneaology
can be picked as the starting point, all in 20 lines of code, not counting db,
the database is flexible and modifiable, adding/deleting new and old data.

all in all it was fun and easy and though frustrating when dealing with typos
and missing bracks, when it works it's like magic!

Mark Tarver

unread,
Dec 28, 2017, 8:03:31 AM12/28/17
to Shen
yes; Prolog is great at this kind of problem.  Such code is always fun to see.

Mark

fuzzy wozzy

unread,
Dec 30, 2017, 5:01:21 PM12/30/17
to Shen
\\ (gp (p)), (gp (c1)), (gp (c2)), (gp (c6))... (findallgp (p)), (findallgp (c1))...

(define p -> [[mark c c1]])
(define c1 -> [[mary c c2] [crystal c c3] [sam c c4]])
(define c2 -> [[frank c c5] [amber] [john]])
(define c3 -> [[tom c c8] [sally c c9]])
(define c4 -> [[eric c c7]])
(define c5 -> [[jonathan] [bob c c6] [robert]])
(define c6 -> [[Maggie]])
(define c7 -> [[suzy]])
(define c8 -> [[tom jr.] [david]])
(define c9 -> [[erica] [Jennifer c c10] [keith]])
(define c10 -> [[kyle]])

(define gp
    P -> [] where (not (child-node P))
    P -> (gpx P []))

(define gpx
    [] Ans -> Ans
    [X|Y] Ans -> (gpx Y (cons gp0 (hdx) ((from1 3 X)) 1 []) Ans)) where (element? c X)
    [X|Y] Ans -> (gpx Y Ans))

(define gp0
    _ [] 1 [] -> []
    GN [] 2 Ans -> Ans
    GN P1 Count Ans -> (gp0 GN (tl P1) 2 (cons [GN -- (nameonly ((from1 3 (hd P1))))] Ans))
                                                                       where (element? c (hd P1))
    GN P1 Count Ans -> (gp0 GN (tl P1) Count Ans))

(define findallgp
    L -> (findallgp0 L []))

(define findallgp0
    [] Ans -> (clean (clean Ans))
    [X|Y] Ans -> (findallgp0 Y (cons [(findallgp ((from1 3 X))) (gp [X])] Ans))
                                         where (and (element? c X) (not (= [] (gp [X]))) (not (= [[]] (gp [X]))))
    [X|Y] Ans -> (findallgp0 Y Ans))

(define nameonly
    L -> (nameonly0 L []))
    [X|Y] Ans -> (nameonly0 Y (cons (hd X) Ans)))

(define child-node
    [] -> false
    [X|Y] -> (if (element? c X) true (child-node Y)))

(define from1
    1 L -> (head L)
    N [X|Y] -> (from1 (- N 1) Y))

(define clean
    [] -> []
    [X|Y] -> (if (or (symbol? X) (number? X)) (append [X] (clean Y)) (append X (clean Y))))

Mark Tarver

unread,
Dec 31, 2017, 7:53:42 AM12/31/17
to qil...@googlegroups.com
Ah - I see the point of the title - no Prolog used!

Mark

--
You received this message because you are subscribed to the Google Groups "Shen" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qilang+unsubscribe@googlegroups.com.
To post to this group, send email to qil...@googlegroups.com.
Visit this group at https://groups.google.com/group/qilang.
For more options, visit https://groups.google.com/d/optout.

fuzzy wozzy

unread,
Feb 14, 2018, 9:27:01 PM2/14/18
to Shen

somehow the parentheses were mismatching, and I actually had to re-type in a new file to make it work,
and (hd X) was typo'ed as (hdx), but what I realized from this program is that shen list used this way has
great capability for depth search, I'm not sure how the prolog program might do, but imagine searching not
just a few generations but a few thousand or a few million generations, well maybe in some role playing
fantasy game, for example, a program like this would work very fast!

Neal Alexander

unread,
Feb 15, 2018, 6:48:30 AM2/15/18
to Shen
What text editor do you use? Matching parens should be semi automatic.

Try paredit or smartparens with emacs, it makes the editor function on tree nodes rather than lines.

fuzzy wozzy

unread,
Feb 15, 2018, 10:23:10 PM2/15/18
to Shen
 
thank you,
I use a notepad that comes with windows os...
I like notepad2 that dr. tarver mentioned here once...
maybe I'll switch to that...

Antti Ylikoski

unread,
Feb 16, 2018, 10:36:07 AM2/16/18
to Shen
Have you tried the notepad++?


I have used it, with the Windows, it is very good.

AJY
The EU
Reply all
Reply to author
Forward
0 new messages