users-prolog
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: if


From: Fergus Henderson
Subject: Re: if
Date: Wed, 31 Jan 2001 06:21:23 +1100

On 31-Jan-2001, Ron Stodden <address@hidden> wrote:
> Fergus Henderson wrote:
> > 
> > How would you write something simple like the following code, then?
> 
> Simple?   What is the underlying operation this tries to achieve -
> say, expressed in simple English?   It is not at all apparent.

The specification is this:

        bag__insert(Bag0, Item, Bag) is true iff
        Bag is the bag that results from inserting Item into Bag0.

Bags are an abstract data type of multisets, represented as maps from
elements to their occurrence count.  `map' is another abstract data
type, with abstract operations that include the predicates
`map__search', to look up a value in a map, and `map__set', to set a
value (non-destructively, i.e. returning as an output the new map).

In English pseudo-code, the implementation is

        bag__insert(Bag0, Item, Bag):
                if Item occurs in Bag0 with count Count0 then
                        Bag is Bag0 with the count for Item replaced
                                by Count0 + 1
                else
                        Bag is Bag0 with the count for Item set to 1

P.S. Here's the original code again.

                % Insert a particular value in a bag.
                % 
        :- pred bag__insert(bag(T), T, bag(T)).
        :- mode bag__insert(in, in, out) is det.

        bag__insert(Bag0, Item, Bag) :-
                (
                        map__search(Bag0, Item, Count0)
                ->
                        Count is Count0 + 1
                ;
                        Count = 1
                ),
                map__set(Bag0, Item, Count, Bag).

-- 
Fergus Henderson <address@hidden>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]