Subject: Re: [SWIPL] Some beginers questions

From: "Richard A. O'Keefe" <[email-masked]>
Date: Wed, 7 Mar 2001 17:05:04 +1300 (NZDT)

iVAN <[email-masked]> asked:

	1.  Does SWI-prolog support RegEx, I didn't found anything in
	the Docs.  If not, is there i way to use them !?!

Which regular expressions do you want?  There are so many to choose from...
At least in UNIX, a regular expression library is part of POSIX.2, so
you should be able to use the foreign function interface to get at it.

Prolog has something much more powerful, namely definite clause grammars.

	2. How can I tokenize text .. example.

As a matter of urgency, buy or borrow a good Prolog book and study it.
The Craft of Prolog has a section on tokenising, but there are other books.

	3.  Is there a way to set a VAR, what i have in mind.  F.e. I
	need to make some tests with lists and want to say something
	like this :

	MyList = [1,4,56,dd,66].
	
	so not to write this every time but pass it like argument to
	list functions i.e. :

	?- liststuff(Result,MyList).

There are no list functions in Prolog, only predicates.
Variables are not things that can be set, nor are they things that can
be global.

What you *can* do is

    ?- assert(l1([1,4,56,16'dd,66])).
    ?- l1(X), list_stuff(Result, X).

	4. Where I can find more info about DCG grammar rules, of cource with more
	examples as for the beginer.
	
From any good Prolog textbook.

	5. What is the difference between call(p) & normal invoking of rule ?! I
	can't figure out :"(
	
You invoke predicates, not rules, just as you call functions in C, not
'if' statements.  The difference is that call(Goal) is used when Goal
is determined at run time.

	6. Is there something like EVAL in Perl. What I mean ?

Yes, call/1.  Prolog syntax, and Prolog evaluation, are not parsed on
strings (unlike Perl and Tcl) nor on lists (unlike Lisp) but on trees.
The data structure that represents the query "is 35 greater than 12"
is (35 > 12), which can also be written as >(35,12) or '>'(35,12).
	Currently I'm using something like this, don't laught :")
	
	cond([X,C,Y]) :-
	    integer(X),
	    integer(Y),
	    cond_type(X, C, Y),
	    writef("Condition %w %w %w met\n", [X,C,Y]).

	cond_type(X,'>',Y) :- X > Y.
	cond_type(X,'<',Y) :- X < Y.
	cond_type(X,'=',Y) :- X = Y.
	
Except for making the discriminating argument the second one instead of
the first one, there is nothing to laugh at here.

In this specific case, you would be better to write
	cond(C) :-
	    C = [X,R,Y],
	    integer(X), integer(Y),
	    compare(R, X, Y),
	    writef("Condition %w %w %w met\n", C).

	7. Is there a way to ALIAS some function.(not so important) f.e.:
	
	alias(pop,last).
	
The number of arguments is part of the name in Prolog.
It's dead simple to do what you want.  Here we go:

	pop(X, Y) :- last(X, Y).

	8. How to print LIST on the screen ?!
	
write('LIST').
If you mean "how to print a list", write(TheList).

	9.  After a lot of experiments, I've got this solution for
	capturing content of (),[],..etc..

What do you mean by "capturing content"?

	next(Res, [H|_]) :- H == ")", Res = [].
	next(Res, [H|T]) :-  next(Res2, T), Res = [ H | Res2 ].
	braces(Res,[H|T]) :- H == "(", next(Res,T).
	
I find this confusing because it is back-to-front.
In Prolog, *INPUTS* precede *OUTPUTS* in the argument list.

Rewriting this,
    braces(["("|T], R) :- next(T, R).

    next([")"|_], []).
    next([H|T], [H|R]) :- next(T, R).

	?- braces(Res,["(",f,g,4,3,")",d,f,g]).
	
	Res = [f,g,4,3]
	
Why are you representing some characters as atoms, some characters
as numbers, and some characters as lists?  Is there any reason why

    braces([0'(|T], R) :- next(T, R).

    next([0')|_], []).
    next([H|T], [H|R]) :- next(T, R).

    ?- braces("(fg43)dfg", R).

could not be used?

	Is this the correct way ?! I still didn't tried parsing this ((...(..))) !!
	
	U may see that the most of the questions are related to PARSING,
	this is coinsidence.  I just can't figured out from where to begin..
	'cause I can't connect all these

	likes(mari,stefan).
	likes(mari,peter).
	?- likes(mari,X).
	
	with some real problem yet :")
	
Read a real Prolog book.  I recommend Sterling & Shapiro to start with.