[mercury-users] A Little Problem With The output: Converting List To List Of Tuples

win1for at yahoo.com win1for at yahoo.com
Sat Dec 18 03:50:20 AEDT 2010


Hi Raphael,

   I am almost there. It compiles well but am not getting the correct results. The results i get now is only one tuple in the list:   [{3, 1}]


Here is the problem.

I have made changes to  the following functions:

I don't understand what u mean by updating Freqs with recursion. i think concatenating [{J,N}]  to it is the update since  OR?. Since it handle one list item at a time as you explained in your previous mail, why do i need to do recursion here?
Can u give me some a little explanation. i think it may be the problem.

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


I have just put inc_freq(X,FreqList)   as the second argument to the last statement. is is correct?:

:- func tabel_aux(list(T), list({T,int})) = list({T,int}).
        tabel_aux([], FreqList) = FreqList.
        tabel_aux([X|Xs], FreqList) =  % inc_freq(X,FreqList)  ++
                        tabel_aux(Xs,inc_freq(X,FreqList)).



Thanks for your help. I appreciate it so much.

Regards,
Eddie.


--- On Fri, 12/17/10, Raphael Collet <rco at missioncriticalit.com> wrote:

From: Raphael Collet <rco at missioncriticalit.com>
Subject: Re: Yet Another Bug:  Converting List To List Of Tuples
To: "win1for at yahoo.com" <win1for at yahoo.com>
Cc: mercury-users at csse.unimelb.edu.au
Date: Friday, December 17, 2010, 11:15 AM

On 12/17/2010 05:02 PM, win1for at yahoo.com wrote:
> Hi Raphael,
>
> I have made the necessary changes and the code look like this. Now i
> have a new error:
>
> 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}).

The compilation error is here:

> tabel([]) = [].
> tabel([X]) = tabel_aux([X], []).

You handle only lists with zero or one element.  You don't handle *any* 
list.  Actually tabel_aux works for any list, so you don't need to do 
pattern matching in tabel...

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

Wait, there is a mistake here above.  You don't update Freqs.  Maybe 
some recursion is needed...

>
>
> :- func tabel_aux(list(T), list({T,int})) = list({T,int}).
> tabel_aux([], FreqList) = FreqList.
> tabel_aux([X|Xs], FreqList) = inc_freq(X,FreqList) ++
> tabel_aux(Xs,FreqList).

I don't understand.  Why are you concatenating frequency lists?
inc_freq(X,FreqList) is the updated frequency list after processing X. 
You should use that one for processing the next element...

>
>
> :- func compare_freq({T,int}, {T,int}) = comparison_result.
> compare_freq({_, N1}, {_, N2}) = Result :-
> compare(Result, N1, N2).
>
> 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).
>



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20101217/d13e7e6f/attachment.html>


More information about the users mailing list