×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Almost works!

## Almost works!

(OP)
Hi,
I'm trying to make this program work :)
we have a list common([a,d,a,c,d,a,b],R). and R=a the most common letter!
so this is how we probably should find it:

common([X|T],E):-  last(T,E),%find the last element (b)
sum([X|T],E,M1),%find how many times that element repeats itself ,count b M1=1
sum([X|T],X,M2),%compare first element of the list with the rest get count(lets say a repeats itself 3 times [a,a,b,a] but sum  only works with the first element of the list and leaves the others)
M2>M1->(M1 is M2 ; change(X,E));    %because a count is 3 we change E to a! and change M1 value to M2 value(something is not right here :/)
common(T,E).
common([_|T],E).

last([Elem], Elem).%last list elem
last([_|Tail], Elem) :- last(Tail, Elem).

change(X,E):-append([],X,E).

sum([X|T],E,M1):- X = E,sum(T,E,C),M1 is 1+C.% count how many times repeats itself
sum([X|T],E,M1):- X \== E,sum(T,E,M1).
sum([],E,0).

### RE: Almost works!

I think you should travel the initial list to obtain a list of tuple (NbLetter, Letter) eg from [a,d,a,c,d,a,b] obtain [(1,b), (1,c), (2,d), (3,a)],  then sort the list in reverse order to obtain [3,a), (2,d), (1,b),(1,b)] and take the head of the list.

### RE: Almost works!

(OP)
Yes you are absolutely right ! but before i do anything i should sort the list sort/2 and use @< @=> because we compare letters :)

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!