This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/Logtalk/library/numberlist.lgt
pmoura 6b4bde14e1 Logtalk 2.30.1 files.
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1903 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
2007-06-12 10:39:47 +00:00

69 lines
1.2 KiB
Plaintext

:- object(numberlist,
implements(numberlistp),
extends(list)).
:- info([
version is 1.1,
author is 'Paulo Moura',
date is 2007/4/20,
comment is 'List of numbers predicates.']).
average([], 0.0).
average([N| Ns], Average) :-
average(Ns, 1, N, Average).
average([], Length, Sum, Average) :-
Average is Sum / Length.
average([N| Ns], Lacc, Sacc, Average) :-
Lacc2 is Lacc + 1,
Sacc2 is Sacc + N,
average(Ns, Lacc2, Sacc2, Average).
min([N| Ns], Min) :-
min(Ns, N, Min).
min([], Min, Min).
min([N| Ns], Aux, Min) :-
( N < Aux ->
min(Ns, N, Min)
; min(Ns, Aux, Min)
).
max([N| Ns], Max) :-
max(Ns, N, Max).
max([], Max, Max).
max([N| Ns], Aux, Max) :-
( N > Aux ->
max(Ns, N, Max)
; max(Ns, Aux, Max)
).
product(List, Product) :-
product(List, 1, Product).
product([], Product, Product).
product([N| Ns], Acc, Product) :-
Acc2 is Acc * N,
product(Ns, Acc2, Product).
sum(List, Sum) :-
sum(List, 0, Sum).
sum([], Sum, Sum).
sum([N| Ns], Acc, Sum) :-
Acc2 is Acc + N,
sum(Ns, Acc2, Sum).
valid(List) :-
nonvar(List),
\+ \+ valid2(List).
valid2([]).
valid2([Head| Tail]) :-
number(Head),
valid2(Tail).
:- end_object.