1
votes

I have facts with numeric attributes (letters with assigned numeric values).

point(a, 1).

point(b, 3).

point(c, 3).

%etc for the rest of the alphabet

I need to write a programme in Prolog that would count these attributes in a list. Instead, now I only managed to count elements in the list, not their attributes. Could you give me any advice? That would help me a lot !

count_points([ ], 0).

count_points([H|T], Count) :-
    count_points(T, Number),
    Count is Number + 1.

The answer should reproduce following example input/output:

?- count_points([h,e,l,p], Score).

    Score = 14. 

I wrote 14, but it depends of the assigned number to the letter.

1
See the online help for formatting. Code excerpts are usually formatted fixed spacing by indenting each line by 4 spaces.lurker
Welcome to Stack Overflow! It looks like you are asking for homework help. While we have no issues with that per se, please observe these dos and don'ts, and edit your question accordingly.Joe C

1 Answers

1
votes

I would go with (see here online):

point(u, 1).
point(r, 2).
point(i, 3).
point(e, 4).
point(l, 5).

count_points([], 0).
count_points([H|T], Count) :- count_points(T, N), point(H, P), Count is N + P.

% count_points([u, r, i, e, l], X).
% X = 15