The compiler tells you that your pattern matching is not exhaustive, and is even kind enough to produce examples that are not matched by any condition in sum_sqrdiffs
. Indeed, you always try to pick the first two elements, so if you use a smaller list as in sum_sqrdiffs []
or sum_sqrdiffs [1]
, one can’t decide what to do. You need to either add a general case, as
 _ > 0
Or you can treat each case separately, if you wish a different behavior for zero or one element. If you add a general case, note that you don’t need your first special case (x::y::[]
) anymore. Side remark : you can totally use the same names in two separate branches of pattern matching (and should do it if it makes sense), so the first would be more natural as x::y::[]
.
However there is a related problem with your algorithm. If we execute it on [1;2;3;4]
then it will proceed as:

sum_sqrdiffs [1;2;3;4]
: Match x::y::z
with x=1
,y=2
,z=[3;4]
. Return (12)^2 + sum_sqr_diffs [3;4]

sum_sqr_diffs [3;4]
: Match x1::x2::[]
with x1=3
,x2=4
. Return (34)^2
You see that you get (12)^2 + (34)^2
instead of the expected (12)^2 + (23)^2 + (34)^2
. The problem is that at each iteration, you indeed need to read the first two elements of the list (the pattern x::y::z
is correct) but you should not throw both x
and y
: y
will be needed to compute the next square. The fix is to make your recursive call on y::z
instead of just z
.