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

win1for at yahoo.com win1for at yahoo.com
Sat Dec 18 03:02:28 AEDT 2010


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}).
        tabel([]) = [].
        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.
        tabel_aux([X|Xs], FreqList) =  inc_freq(X,FreqList)  ++
                        tabel_aux(Xs,FreqList).


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

*********************************************************************************************

the error is, i think am missing something somewhere :

oplossing1.m:011: In `tabel(in) = out':
oplossing1.m:011:   error: determinism declaration not satisfied.
oplossing1.m:011:   Declared `det', inferred `semidet'.
oplossing1.m:013:   In argument 1 of clause head:
oplossing1.m:013:   in argument 2 of functor `[|]/2':
oplossing1.m:013:   unification with `list.[]' can fail.


Thanks for your reply. Hope you will help me fix it.

Regards,
Eddie









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

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

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



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


More information about the users mailing list