I'm fairly new to Prolog. Anyway, I'm trying to write a recursive set of rules that return the average number of characters per word in a given list of character codes. My code is below.
medellangd(Text,AvgLen) :-
medellangd(Text,T,1,0,0),
AvgLen = T.
medellangd([],AvgLen,Space,Words,Chars) :-
T is (Chars/Words),
AvgLen = T.
medellangd([A|B],AvgLen,Space,Words,Chars) :-
succ(Chars,C),
updatewords(A,Space,Words,W),
updatespace(A,S),
medellangd(B,T,S,W,C),
AvgLen = T.
updatewords(A,1,Words,W) :-
member(A, [65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122])
-> succ(Words,S),
W = S
; W = Words.
updatewords(A,0,Words,W) :-
W = Words.
updatespace(A,S) :-
member(A,[65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122])
-> S = 0
; S = 1.
For reasons which I cannot tell, although AvgLen gets the right value, Prolog returns false when I call medellangd([68,69],AvgLen). When I trace this call, although every call initially is exited before AvgLen gets its value, Prolog decides to redo "(9) updatewords(68, 1, 0, _G2574)" if I enter a semicolon after the AvgLen value assignment, and fails. Why does this happen?