Advertisement
C4Cypher

Making the most out of aggregate

Aug 8th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.49 KB | None | 0 0
  1.  
  2.     % count(Count, First, Last, Index):
  3.     %
  4.     % Count the number of values in an index, providing the first and last
  5.     % indexes.
  6.     %
  7. :- pred count(int::out, int::out, int::out, T::in) is det.
  8.  
  9. count(C, F, L, T) :-
  10.     ( A = func(I, U@{Count, First0, Last0}) = {Count + 1, First, Last} :-
  11.         ( U = {0, 0, 0} ->
  12.             First = I, Last = I
  13.         ;
  14.             First = (First0 < I -> First0 ; I),
  15.             Last = (Last0 > I -> Last0 ; I)
  16.         )
  17.     ),
  18.     (P = pred(I) :- index(I, T)),
  19.     aggregate(P, A, {0, 0, 0}, {C, F, L}).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement