view 3-Prolog/day2-maths.pl @ 67:8906b5a4517f

Calculate and validate boxes The solver now creates answers for valid sudoku, but doesn't reject invalid ones (e.g. dupe in column)
author IBBoard <dev@ibboard.co.uk>
date Sat, 07 Oct 2017 15:07:34 +0100
parents f86bb0d669be
children
line wrap: on
line source

% First use of variable assignment!
% We use a mix of rules and facts to do maths on lists.
count(0, []).
% This seems backwards, but we're saying "Count of a list with a head and a tail
% is the count of its tail (which will be assigned to TailCount) plus one, which
% is then assigned to Count, which gets passed back out"
count(Count, [Head|Tail]) :- count(TailCount, Tail), Count is TailCount + 1.

sum(0, []).
sum(Total, [Head|Tail]) :- count(Sum, Tail), Total is Head + Sum.

average(Average, List) :- sum(Sum, List), count(Count, List), Average is Sum / Count.