new to prolog - need help
Author Message
new to prolog - need help

I need to relate a structure to another structure i this way:

thing(K,S):-
relation(K),
secondrelation(S).

largerthing([thing(A,B),thing(C,D).........]).

how do i get the larger thing to see the relationship for the other
thing without going through and saying relation(A),
relation(C).....etc??

this is for SWI-prolog

Ta

Mon, 18 Feb 2002 03:00:00 GMT
new to prolog - need help
Dear Karen,

Quote:
> I need to relate a structure to another structure i this way:

> thing(K,S):-
>     relation(K),
>     secondrelation(S).

> largerthing([thing(A,B),thing(C,D).........]).

> how do i get the larger thing to see the relationship for the other
> thing without going through and saying relation(A), relation(C).....etc??

There is a difference between structures and predicates. Your first use of "thing" is as a predicate. In the predicate "largerthing" you have a structure (a list) with members which are structures (things). It's OK to use the same name for predicates and structures, but keep in mind which is which.

One question you have to answer is whether the empty list is a largerstructure. If so, then

largerstructure([]).

is true.

Then you can define the predicate, largerstructure/1 (the 1 indicates the arity), like this:

largerstructure([]).
largerstructure([thing(A,B)|Things]) :-
thing(A,B),
largerstructure(Things).

The first clause says that the empty list is a largerstructure. The second clause says that the list, [thing(A,B)|Things], is a largerstructure if thing(A,B) is true, and Things is a largerstructure.

Note that in the second clause "thing(A,B)" appears first as a structure, second as a predicate.

If the empty list is not a largerstructure, but a list with a single thing is, then the first clause may be

largerstructure([thing(A,B)]) :- thing(A,B).

Best regards,

Bill Spight

P. S. For email remove the x.

Mon, 18 Feb 2002 03:00:00 GMT

 Page 1 of 1 [ 2 post ]

Relevant Pages