简体   繁体   中英

Prolog : counting values in a list

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM