root/trunk/library/numberlist.lgt

Revision 4482, 1.2 KB (checked in by pmoura, 7 weeks ago)

Modified the implementation of the predicate product/2 in the library object "numberlist" to fail for empty lists.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1
2:- object(numberlist,
3    implements(numberlistp),
4    extends(list)).
5
6    :- info([
7        version is 1.3,
8        author is 'Paulo Moura',
9        date is 2008/9/15,
10        comment is 'List of numbers predicates.']).
11
12    average([], 0.0).
13    average([N| Ns], Average) :-
14        average(Ns, 1, N, Average).
15
16    average([], Length, Sum, Average) :-
17        Average is Sum / Length.
18    average([N| Ns], Lacc, Sacc, Average) :-
19        Lacc2 is Lacc + 1,
20        Sacc2 is Sacc + N,
21        average(Ns, Lacc2, Sacc2, Average).
22
23    min([X| Xs], Min) :-
24        min(Xs, X, Min).
25
26    min([], Min, Min).
27    min([X| Xs], Aux, Min) :-
28        (   X < Aux ->
29            min(Xs, X, Min)
30        ;   min(Xs, Aux, Min)
31        ).
32
33    max([X| Xs], Max) :-
34        max(Xs, X, Max).
35
36    max([], Max, Max).
37    max([X| Xs], Aux, Max) :-
38        (   X > Aux ->
39            max(Xs, X, Max)
40        ;   max(Xs, Aux, Max)
41        ).
42
43    product([X| Xs], Product) :-
44        product(Xs, X, Product).
45
46    product([], Product, Product).
47    product([X| Xs], Acc, Product) :-
48        Acc2 is Acc * X,
49        product(Xs, Acc2, Product).
50
51    sum(List, Sum) :-
52        sum(List, 0, Sum).
53
54    sum([], Sum, Sum).
55    sum([X| Xs], Acc, Sum) :-
56        Acc2 is Acc + X,
57        sum(Xs, Acc2, Sum).
58
59    valid(-) :-     % catch variables and lists with unbound tails
60        !,
61        fail.
62    valid([]).
63    valid([Element| List]) :-
64        number(Element),
65        valid(List).
66
67:- end_object.
Note: See TracBrowser for help on using the browser.