[mercury-users] A Bug: Converting List To List Of Tuples

Raphael Collet rco at missioncriticalit.com
Sat Dec 18 02:52:13 AEDT 2010


On 12/17/2010 03:57 PM, win1for at yahoo.com wrote:
> Hi Raphael,
>
> I have pieced all the pieces together except the sorting part. I get
> some errors.This is the code:
>
>
> - module oplossing1.
>
> :- interface.
> :- import_module io.
>
> :- pred main(io.state::di, io.state::uo) is det.
>
> :- implementation.
> :- import_module int, list,string,map.
>
> :- func tabel(list(T)) = list({T,int}).
> tabel([X]) = tabel_aux([X], []).
>
>
> :- func inc_freq(T, list({T,int})) = list({T,int}).
> inc_freq(I, []) = [{I,1}].
> inc_freq(I, [{J,N}|Freqs]) = (if (I = J)
> then [{J,N+1}|Freqs] % I's counter incremented, rest untouched
> else Freqs ++ [{J,N}] % keep J in the list, but increment the
> ). % frequency of I in Freqs
>
>
> :- func tabel_aux(list(T), list({T,int})) = list({T,int}).
> tabel_aux([], FreqList) = FreqList.

This second clause is not valid:

> tabel_aux([X|Xs], FreqList) = (inc_freq(X,FreqList) ,
> tabel_aux(Xs,FreqList)).

inc_freq(X,FreqList) returns a value, which is a 'new' FreqList.  What 
do you do with that value?

>
>
> main(!IO):-
> %io.print(tabel(string.to_char_list("this is a test")),!IO),
> io.nl(!IO),
> io.print(tabel([3,2,1,2,1,1,2]),!IO),
> io.nl(!IO).
>

Otherwise the rest looks ok.

Cheers,
Raphael
--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the users mailing list