hi jeremy, all,

here is one simple example , let say we have this XML file:
<root>
 <unit>
  <q></q>
  <answer><tag></tag></answer>
 </unit>
 <unit>
  <q></q>
  <answer>
     <desc>
      <code></code>
     </desc>
  </answer>
 </unit>
 <unit>
  <q></q>
  <answer>
     <code></code>
  </answer>
 </unit>
</root>


how we can implement the following XPath expression - "file://code"
I'm giving here very simplified example (orthen works as shown in first
interpretation i.e.) :

if "block1" return true(1) the end result is true(1)
but if "block1" return false(0) "block2" is evaluated then
     if "block2" return true(1) the end result is true(1)
     if "block2" return false(0)
             we evaluate "block1" again i.e. start from the beginning.


===========================================
node stores STATE information...to be easy ...
don't take care about the syntax...or some small semantic errors...

sub getNextChildNode
 {
   my $node = shift;
    if ($node->{visited}) {return 0}
     else


     $node->{currchild}++;
   return getchild($node)
   };
 };

my $node = ROOT;
{ getNextChildNode($node) orthen  getParentNode($node) }
   andthen { is this <code>  add to the result; $node->visited(1)};

OK.What is happening

1.I'm in the root
2. Give me the ROOT child -> context changes to first <unit>
3. Is this <code> -> no
4. Give me the next node child -> context changes to first <q>
5. Is this <code> -> no
6. Give me the next  node child -> no childs
7. Give me the parent ->  context changes to first <unit>
8. Give me the next node child -> context changes to first <answer>
9. Is this <code> -> no
10. Give me the next  node child -> no childs
11. Give me the parent ->  context changes to first <unit>
12. Give me the next  node child -> no MORE childs
13. Give me the parent ->  context changes to first <root>
14. Is this <code> -> no
15. Give me the next  node child -> context changes to second <unit>
...and so on .... did U gotcha the idea ...
(pls correct me if I'm wrong)

this code will traverse the whole TREE and will find all <code> elements...
similar approach can be used for directories or all others TREE
structures...
I will try in the next RFC to give more examples... sorry for this, but my
Perl is far-better than Prolog or C.

Shorter : "Get Parent or Child and check for <code>"
 every node has two connections, one of them CHILD(is array of nodes) other
parent
[ declarative,natural semantic the way human think not the way machine
think ]

The main idea is to think about this not as algorithm(step-by-step
instructions, how this will be done, "the way" if you use while,until or do
cycles), but on the following way :
"the <code> tag should be somewhere let me check all childs and parents of
my nodes"

THIS is called DECLARATIVE SEMANTICS and is the PROLOG biggest strength, the
drawback of this type of programming is sometimes the speed ... but when you
want speed you can combine - PROCEDURE and DECLARATIVE semantics.
Example : PROCEDURE (HOW-TO), DECLARATIVE (FAQ).
If someone can explain this better, please do it - my English is not so
good.thanx.

I think the equivalent will be :

sub walk
 {
   my $node = shift;
   if ($node->name eq "code") {do something; return};
   while (getNextChildNode)
    {
   if ($_)  { walk($_) }
    else {getParent}
 }
 }

but think again this is not normal way of solving the problem i.e. U use
recursion does
the "forward flow" programming has the notion of the recursion i.e. describe
the problem
"with yourself" - NO ? This is the way human think...So the BACKTRACKING is
yet another way humans thinks.
Someone will say - Yes but I can do it faster w/o backtracking.
The answer - It will be alot faster if U use Assembler, isn't it ?:")

Other benefits :

Yet another way to get rid of this "if-elseif-elseif" construction, this is
my most hated
construction (I've never used it and will not :")). Can U believe that there
is a people
that can make 25 screens if-elseif-...-elseif contruction, I have a bad luck
to correct
such a beast :"(

Think of THREADS, this way of programming is much more threads-friendly

Other areas of usage : Lexers,Parsers,REGEX's

I'm also a very very ... very beginner in Prolog :"( so don't worry.

PS.......................

Yet another getNextChildNode :")

sub getNextChildNode
 {
   my $node = shift;
   ! $node->{visited} andthen
 {
   $node->{currchild}++
   orthen
      return getchild($node)
 }
 };


 HtH
=====
iVAN
[EMAIL PROTECTED]
=====

Reply via email to