Re: Can I do something like this
"Richard A. O'Keefe" <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
On 24/03/2014, at 9:05 AM, Roelof Wobben wrote:
> I have a predicate which has 2 possibilities.
>
> the parameter is substained or not.
What the dickens does "sustained" mean?
I am getting a strong impression that you are trying
to learn Prolog by hacking away without studying any
book or any existing code.
> Can I do something like this now.
>
> my_predicate(var(x)) :-
> write("The solutions are").
>
> my_predicate(nonvar(x) )
> write("the pairs are").
That is legal syntax, but it appears that you
have not yet understood what patterns are or
how pattern matching works.
That is astonishing, because it is described
clearly early on in books like "Programming
in Prolog" (Clocksin & Mellish), "Clause and
Effect" (Clocksin), "The Art of Prolog"
(Sterling & Shapiro), and practically every
Prolog book I've ever seen. There are
course notes on the web as well.
var(x) as a pattern matches var(x) and nothing
else. nonvar(x) as a pattern matches nonvar(x)
and nothing else.
Do a web search for (Prolog course notes).
Read. Come back when you understand _why_
these patterns do what I say they do.
>
> Or can I better do :
>
> my_predicate(x) :-
> var(x),
> write ( ).
>
> my_predicate(x) :-
> nonvar(x),
> write ( ).
This predicate accepts _only_ the term 'x' as its
argument. The first clause will always fail because
'x' is not a variable, so var(x) will fail.