Reputation: 3
I'm a beginner Prolog/SWI Owl programmer and would like to calculate the score of words entered in SWI Owl by counting the points that each letter of the word has - just like scrabble!
The points for each word would be stated in the form of facts, like these:
letter_point(a, 1).
letter_point(b, 3).
letter_point(c, 3).
letter_point(d, 2).
% etc.
The numerical arguments of these facts should be counted using the predicate 'scrabble_score/2' with the word (list) and total score (number) as arguments, in order to get the following result:
?- letters_score([a, b, c, d], Score).
Score = 9.
I'm assuming this could be done with 'is/2' to count the numerical arguments of each letter in the list, but I cannot figure out yet how to get there. Any help as to how to do this would be highly appreciated!
Best, Lara
Upvotes: 0
Views: 167
Reputation: 573
easy solution
% foo(Arg1,Arg2)
foo(a,1).
foo(b,2).
foo(c,3).
sum_foo_from_list([],0).
sum_foo_from_list([Arg1|Rest],Sum_Arg2) :-
% ?Arg2
foo(Arg1,Arg2),
% #= (clpfd library)
Sum_Arg2 #= Arg2 + Sum_Arg2_,
% Sum_Arg2 is diffrent to Sum_Arg2_
sum_foo_from_list(Rest,Sum_Arg2_).
test :
| ?- sum_foo_from_list([a,b],N).
N = 3
Upvotes: 1