For Programmers: Free Programming Magazines  


Home > Archive > Prolog > September 2004 > compare element in a list prolog









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author compare element in a list prolog
kimos

2004-09-23, 8:59 pm

hi all,
im new to prolog

and im trying to make this pred.

compare[a,a,a,a].
yes


compare[a,e,a,t].
no


so if the element of the list are the same then its a yes if not then its a no

how can i do this?

i was trying this but it didnt work

compare([H|T]):-
element(H, [H|T]).
element(H, [H|T]):-
element(H,T).

but this is wronge. anyone can help me plz?
david emile lamy

2004-09-24, 3:58 am

urdad@hotmail.com (kimos) writes:

> hi all,
> im new to prolog
>
> and im trying to make this pred.
>
> compare[a,a,a,a].
> yes
>
>
> compare[a,e,a,t].
> no
>
>
> so if the element of the list are the same then its a yes if not then its a no
>
> how can i do this?
>
> i was trying this but it didnt work
>
> compare([H|T]):-
> element(H, [H|T]).
> element(H, [H|T]):-
> element(H,T).
>
> but this is wronge. anyone can help me plz?


What is element's base case? It needs a fact to know if the recursive
procedure is successful by stating how the final step should appear.

Hopefully this hint will provoke you to add a simple line of code.
--
gnupg fingerprint: 4055 65EC 24D1 1DC2 700C 6654 6009 4AB9 F4C3 3E7E
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com