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

Raphael Collet rco at missioncriticalit.com
Sat Dec 18 04:36:16 AEDT 2010

```On 17/12/10 17:50, win1for at yahoo.com wrote:
>
> 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?

Just concatenating [{J,N}] does not update the frequency of I, since you
have not found I in the frequency list yet.  You have to recurse through
the element of the frequency list in order to find the tuple to update
(or to add if the item is not present).

> 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)).

You'll see when the other one is correct :-)

I will no longer be available, now.  My weekend just started...

Cheers,
Raphael
--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au