Help with basics... 
Author Message
 Help with basics...

Er hi.

Sorry about this, but I'm using prolog for the
first time, and I want to know about basic list
processing.

All I want to do is write a predicate that takes
a list as it's only parameter and will succeed
if (and only if) the list has two consective
elements which are equal.

I think it should basically look like this :

two_same(LIST):-SOMETHING GOES HERE ?

but I'm not really sure.

Thank you all in advance.



Mon, 12 Jul 1999 03:00:00 GMT  
 Help with basics...

Quote:

> Er hi.

> Sorry about this, but I'm using prolog for the
> first time, and I want to know about basic list
> processing.

> All I want to do is write a predicate that takes
> a list as it's only parameter and will succeed
> if (and only if) the list has two consective
> elements which are equal.

> I think it should basically look like this :

> two_same(LIST):-SOMETHING GOES HERE ?

> but I'm not really sure.

> Thank you all in advance.

We check the first two items, if they are the same we're done.  If not
we loose the head of the least and process the tail. Eventually we
either find that two are the same or the clause fails. As in:

// If first two are same, we are done.
two_same(X,X | _).
// Loose the head and try again.
two_same(_,Tail) :-
  two_same(Tail).

BTW -- note that this clause may have some unexpected backtracking since
it will keep track of an untried alternative for each remaining element
in the tail.  If you want to check the list one time and one time only,
change the first clause to:

// If first two are same, we are done and don't try again.
two_same(X,X | _ ) :- !.

Good luck!
--Lou



Tue, 13 Jul 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Please help Visual Basic Programer with simple read/write text file routine---Real Basic 2.1

2. Help! Utter newbie needs help with basic code

3. Need help with basic in Exel

4. Help with basic template problem

5. help using BASIC (gwbasic, basica or qbasic)

6. Need Help : a basic scheme problem ( thanks in advancd )

7. Help with basic M2

8. Help- Visual Basic

9. Help w/Basic Cobol programming

10. HELP : Visual Basic calling FORTRAN DLL

11. HELP : visual basic calling fortran DLL

12. HElp getting basics down.

 

 
Powered by phpBB® Forum Software