[mercury-users] io__foldl

Nancy Mazur Nancy.Mazur at cs.kuleuven.ac.be
Sat Nov 16 02:45:00 AEDT 2002


Hi Noel, 

* Noel  Pinto <cool4life at rediffmail.com> [2002-11-15 16:15]:
> Hi,
> 
> In the string module string__foldl is defined as...
> 
> :- func string__foldl(func(char, T) = T, string, T) = T.
> :- pred string__foldl(pred(char, T, T), string, T, T).
> :- mode string__foldl(pred(in, in, out) is det, in, in, out) is 
> det.
> :- mode string__foldl(pred(in, di, uo) is det, in, di, uo) is 
> det.
> :- mode string__foldl(pred(in, in, out) is semidet, in, in, out) 
> is semidet.
> :- mode string__foldl(pred(in, in, out) is nondet, in, in, out) is 
> nondet.
> :- mode string__foldl(pred(in, in, out) is multi, in, in, out) is 
> multi.
> %	string__foldl(Closure, String, Acc0, Acc):
> %	`Closure' is an accumulator predicate which is to be called for 
> each
> %	character of the string `String' in turn. The initial value of 
> the
> %	accumulator is `Acc0' and the final value is `Acc'.
> %	(string__foldl is equivalent to
> %		string__to_char_list(String, Chars),
> %		list__foldl(Closure, Chars, Acc0, Acc)
> %	but is implemented more efficiently.)
> 
> 
> Closure is said to be an accumulator predicate which means that it 
> stores the intermediate results.

Strange definition. Closure is a predicate which takes a char and
something of your own type T as inputs, and generates a new thing of
your type T. 

Perhaps, instead of trying to immediately use these predicates, it would
be good to be confronted with a couple of assignments which, at the end
should give you a feeling that you're doing the same thing, but each
time slightly differently. And then, you might recognize the
similarities, and better understand the use of foldl (whether
list__foldl, or string__foldl, or any other higher order predicate). 

Simple assignments could be: 
- writing a predicate that returns the number of occurrences of the
  character 'a' in a string. 
- writing a predicate that returns the number of digits appearing in a
  string...

Already these two little silly exercices should give you a better idea on
the use of a thing such as 'foldl'. 

Try to implement these as an exercise... if you still don't have a clue
on the use of foldl, try to implement other similar assignments... 



> I would like to know how do I use it along with string__foldl.
> 
> Should I create a type like
> :- type silly_type --> number(int); couple(int, int)
> or a similar type

> 
> and a predicate
> :- pred print_silly_type(silly_type, io__state, io__state).
> :- mode print_silly_type(in, di, uo).
> or  aa similar predicate
> 
> and implement it as
> print_silly_type(MyType) -->
> % with some implementation here.
> 
> Should I do all these??? The reason is, I am getting an idea that 
> the accumulator predicate has to be used.
> BUT, accumulator predicate stores the intermediate results, I feel 
> it should not be done.

no, predicates don't store anything... they compute something perhaps... 

> IS IT REALLY needed to use them?? Are they the right types and 
> predicates??

your question is pointless. Are they the right types? For doing what? 

> If yes plz do tell me how in reality it can be done. I am 
> confused. Plz some code to illustrate it. A small code will do.

No, I think it is better if you first try to implement something
easier first... like those two little exercices I mentioned above.. 

Nancy
--------------------------------------------------------------------------
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