[mercury-users] Questions on the counter class

Terrence Brannon princepawn at earthlink.net
Fri Apr 20 12:49:46 AEST 2001


Here are my questions regarding a portion of this class:

  % counter__allocate(N, Counter0, Counter) takes a counter, and
        % returns (a) the next integer to be allocated from that
        counter,
        % and (b) the updated state of the counter.

:- pred counter__allocate(int::out, counter::in, counter::out) is det.

:- implementation.

:- import_module int.

:- type counter ---> counter(int).

counter__init(N) = counter(N).

counter__init(N, counter__init(N)).

counter__allocate(N, counter(N), counter(N + 1)).


1 - When the docs say that counter__allocate returns something,
this isn't true. This is a predicate. It doesn't return
anything. What it does do is bind two out parameters.

2 - Looking at the implementation for counter__allocate, it appears that
int::out is bound to N, not N+1, thus I don't see how the docs could
be right on this point when they say "(a) the next integer to be allocated from that
counter," --- N is the _current_ integer not the next one.

3 - I would like to see a sample usage of this counter. What confuses
me is how to extract the current value of the counter. How would I do
the following:

Create a random number, R.
Create a counter which will start at 0.
Iterate up to R. On each iteration:
  print the value of the counter.
  increment the counter
  terminate when the value of the counter is equal to R
 
I wrote a version of this without the counter (criticisms welcomed)
but would be interested in seeing an implementation with the counter class.

rand_print() :-
  rand_num(R),
  rand_print_2(R,0).

rand_print_2(R,D) :-
  (
     R =  I -> fail
     ;
     R >  I -> 
        io__write_nl(D),
	rand_print_2(I,D+1)
     ;
     error("the random number is larger than I")
  )

  







--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list