% counts # of words in stirng
wordc([], 0).
wordc([A], WC) :- % A is part of a word at the end of the list.
is_alpha(A)
-> WC is 1
; WC is 0, !.
wordc([A,B|AS], WC) :- % A is the last char of a word.
is_alpha(A),
\+ is_alpha(B),
wordc(AS, WC1),
WC is WC1 + 1, !.
wordc([_|AS], WC) :- % A is a char within a word or A is a space
wordc(AS, WC1),
WC is WC1, !.
%counts # of chars in string
charc([], 0).
charc([A|AS], CC) :-
is_alpha(A),
charc(AS, CC1),
CC is CC1 + 1, !.
charc([A|AS], CC) :-
\+ is_alpha(A),
charc(AS, CC1),
CC is CC1 + 0, !.
medellangd([], 0).
medellangd(Text, AvgLen) :-
wordc(Text, WC), charc(Text, CC),
AvgLen is (CC / WC), !.