% gpa.pl
%
% Find gpa given 4 letter grades: a=4, b=3, c=2, d=1.
gpa(List) :- gpa(List, 0).
gpa([], Sum) :- write(Sum), nl, G is Sum/4, write(G).
gpa(List, Sum) :- head(List,First), compare(First,a), S is Sum+4, write(S), nl, tail(List, Tail), gpa(Tail,S) ; 
                        head(List,First), compare(First,b), S is Sum+3, write(S), nl, tail(List, Tail), gpa(Tail,S);
                        head(List,First), compare(First,c), S is Sum+2, write(S), nl, tail(List, Tail), gpa(Tail,S);
                        head(List,First), compare(First,d), S is Sum+1, write(S), nl, tail(List, Tail), gpa(Tail,S), !.
head([H|_T], H).
tail([_H|T], T).
compare(X,X).
