<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Hi Raphael,<br><br>      I have pieced all the pieces together except the sorting part. I get some errors.This is the code:<br><br><br>- module oplossing1.<br><br>:- interface.<br>:- import_module io.<br><br>:- pred main(io.state::di, io.state::uo) is det.<br><br>:- implementation.<br>:- import_module int, list,string,map.<br><br>:- func tabel(list(T)) = list({T,int}).<br>        tabel([X]) =  tabel_aux([X], []).<br><br><br>:- func inc_freq(T, list({T,int})) = list({T,int}).<br>        inc_freq(I, []) = [{I,1}].<br>    inc_freq(I, [{J,N}|Freqs]) =   (if (I =
 J)<br>                                          then [{J,N+1}|Freqs]   % I's counter incremented, rest untouched<br>                          else Freqs ++ [{J,N}]       % keep J in the list, but increment the<br>                       ).               % frequency of I in Freqs<br>                      <br><br>:- func tabel_aux(list(T), list({T,int})) =
 list({T,int}).<br>        tabel_aux([], FreqList) = FreqList.<br>        tabel_aux([X|Xs], FreqList) =  (inc_freq(X,FreqList) , <br>                       tabel_aux(Xs,FreqList)).<br><br><br>main(!IO):-<br>%io.print(tabel(string.to_char_list("this is a test")),!IO),<br>io.nl(!IO),<br>io.print(tabel([3,2,1,2,1,1,2]),!IO),<br>io.nl(!IO).<br><br><br><br>The error is in the tabel_aux. The comma(",") between the two statements in the body of the function causes the compile error.<br><br>The error i receive is:<br><br>oplossing1.m:025:   In clause for function `oplossing1.tabel_aux/2':<br>oplossing1.m:025:   in function result term of clause head:<br>oplossing1.m:025:   error: the language construct ,/2 should be<br>oplossing1.m:025:   used as a goal, not as
 an expression.<br><br>I hope you will help me to fix it.<br><br>Thanks for your help.<br><br>Regards,<br>Eddie<br><br>--- On <b>Fri, 12/17/10, Raphael Collet <i><rco@missioncriticalit.com></i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: Raphael Collet <rco@missioncriticalit.com><br>Subject: Re: [mercury-users] Converting List To List Of Tuples<br>To: "win1for@yahoo.com" <win1for@yahoo.com><br>Cc: mercury-users@csse.unimelb.edu.au<br>Date: Friday, December 17, 2010, 8:50 AM<br><br><div class="plainMail">Dear Eddie,<br><br>Okay, I will give you some guidance.  Maybe we should leave the map <br>aside, and use a simpler data structure to count frequencies.  My <br>proposal is to count frequencies directly with a list({T, int}).<br><br>Assume you have the string "a ball".  You start off with an empty list <br>of frequencies.  Here is how the
 frequencies are updated while you <br>process the items:<br><br>initially:    -> []<br>process 'a'    -> [{'a',1}]<br>process ' '    -> [{'a',1},{' ',1}]<br>process 'b'    -> [{'a',1},{' ',1},{'b',1}]<br>process 'a'    -> [{'a',2},{' ',1},{'b',1}]<br>process 'l'    -> [{'a',2},{' ',1},{'b',1},{'l',1}]<br>process 'l'    -> [{'a',2},{' ',1},{'b',1},{'l',2}]<br><br>For each item I, you call a function inc_freq(I, FreqList) which returns <br>the FreqList with the item's counter incremented.  If the item is not <br>present, the element {I,1} is added to FreqList.<br><br>% return the frequency list with the given item's frequency incremented<br>:- func inc_freq(T, list({T,int})) = list({T,int}).<br><br>inc_freq(I, []) = [{I,1}].<br>inc_freq(I, [{J,N}|Freqs]) =<br>     ( I = J -><br>     
    [{J,N+1}|Freqs]   % I's counter incremented, rest untouched<br>     ;<br>         ?                 % keep J in the list, but increment the<br>                           % frequency of I in Freqs<br>     ).<br><br>Fill in the question mark above.  Check your solution on paper with the <br>example above.<br><br>Now that you have this function, you have to apply it on each element of <br>the list.  Write an auxiliary function tabel_aux with an extra argument, <br>the frequency list that is updated through the process.<br><br>% tabel_aux(Items, Freqs) adds the frequencies of Items to Freqs<br>:- func tabel_aux(list(T), list({T,int})) = list({T,int}).<br><br>tabel_aux will apply inc_freq to each element of the list, and
 return <br>the final result, i.e.,<br><br>tabel_aux([X, Y, Z], FreqList) =<br>     inc_freq(Z, inc_freq(Y, inc_freq(X, FreqList)))<br><br>Write tabel_aux as a recursive function.  By applying it on an empty <br>frequency list, you will get the items' frequencies.  What remains to be <br>done is to sort the list to get the less frequent items first.  Let us <br>use the function list.sort/2:<br><br>tabel(Items) = sort(compare_freq, tabel_aux(Items, [])).<br><br>The function sort takes the function compare_freq as a parameter.  The <br>latter compares two frequency tuples, and returns (<), (>), or (=) <br>depending on how they compare.  You can compute that result by applying <br>the predicate compare on the frequencies:<br><br>% return the comparison of two frequency tuples<br>:- func compare_freq({T,int}, {T,int}) = comparison_result.<br><br>compare_freq({_, N1}, {_, N2}) = Result :-<br> 
    compare(Result, N1, N2).<br><br>Now, glue up all together, and you're done.<br><br><br>Cheers,<br>Raphael<br><br><br>On 12/17/2010 01:02 PM, <a ymailto="mailto:win1for@yahoo.com" href="/mc/compose?to=win1for@yahoo.com">win1for@yahoo.com</a> wrote:<br>> Hi Raphael,<br>><br>> Thank you so much for your reply. I really appreciate it. I have been<br>> taught Mercury for only 4 hours and am suppose to do that task. My only<br>> problem now is how to begin. Infact i really want to do it myself so<br>> that i can learn from it. So i will ask you specific questions to help<br>> me get the procedure so that i can do it myself. This is how it is.<br>><br>> The function siganature is this:<br>><br>> tabel(list(T)) = list({T,int})<br>><br>> (it must work with both lists of characters and ints). The results -<br>> (lists of tuples) - The first element in each tuple is an element from<br>> the list and
 the second element is the frequentie of the element in the<br>> list.<br>><br>> so my first line of my implementation is this:<br>><br>> tabel([X|Xs]) =<br>><br>><br>> Now these are my concerns::<br>><br>> Do i have to go through the list and set the keys of the map with the<br>> elements in the list. Meaning that the values will be empty and then<br>> later i will go over the list again and set the values(frequenties) ?<br>><br>> I would like you to give me the steps i should follow and solve it. For<br>> instance you mentioned functions like:<br>><br>> map.set/4<br>> map.set/3<br>> string.foldl/4<br>> assoc_list<br>> map<br>> pair<br>><br>> in your replies. Can you please explain to me how all these functions<br>> fit in so that i can understand the procedure and do it. I need the<br>> steps so that i can do it. I have good understanding from your replies<br>> so what i
 need now is the steps to follow and get it done<br>><br>> Thank you so much for your time and concern.<br>><br>> Regards,<br>> Eddie<br>><br>><br>><br>><br>> --- On *Fri, 12/17/10, Raphael Collet /<<a ymailto="mailto:rco@missioncriticalit.com" href="/mc/compose?to=rco@missioncriticalit.com">rco@missioncriticalit.com</a>>/* wrote:<br>><br>><br>>     From: Raphael Collet <<a ymailto="mailto:rco@missioncriticalit.com" href="/mc/compose?to=rco@missioncriticalit.com">rco@missioncriticalit.com</a>><br>>     Subject: Re: [mercury-users] Converting List To List Of Tuples<br>>     To: <a ymailto="mailto:mercury-users@csse.unimelb.edu.au" href="/mc/compose?to=mercury-users@csse.unimelb.edu.au">mercury-users@csse.unimelb.edu.au</a><br>>     Cc: "<a ymailto="mailto:win1for@yahoo.com"
 href="/mc/compose?to=win1for@yahoo.com">win1for@yahoo.com</a>" <<a ymailto="mailto:win1for@yahoo.com" href="/mc/compose?to=win1for@yahoo.com">win1for@yahoo.com</a>><br>>     Date: Friday, December 17, 2010, 6:33 AM<br>><br>>     Dear anonymous user,<br>><br>>     What you ask for looks a lot like a homework... I ain't going to do the<br>>     homework for you. Here are a few hints, though.<br>><br>>     The function map.init returns an empty map. That's where you should<br>>     start from.<br>><br>>     The predicate map.set/4 (and the function map.set/3) allows you to set<br>>     the value associated to a key in the map. In your case, this could be<br>>     the current frequency of a character.<br>><br>> 
    The predicate map.search/3 looks up for a given key, and return the<br>>     corresponding value. You can use this to retrieve the frequency of a<br>>     character. The typical use-case is:<br>><br>>     ( map.search(Map, Key, Value) -><br>>     % do something with the value<br>>     ;<br>>     % no value for key, do something else<br>>     )<br>><br>>     Cheers,<br>>     Raphael<br>><br>>     On 12/17/2010 11:56 AM, <a ymailto="mailto:win1for@yahoo.com" href="/mc/compose?to=win1for@yahoo.com">win1for@yahoo.com</a><br>>     </mc/compose?to=<a ymailto="mailto:win1for@yahoo.com" href="/mc/compose?to=win1for@yahoo.com">win1for@yahoo.com</a>> wrote:<br>>   
   ><br>>      > Please am a total beginner can you give me an example of how to<br>>     use the map.<br>>      ><br>>      > thanks for your reply<br>>      > --- On *Fri, 12/17/10, Raphael Collet /<<a ymailto="mailto:rco@missioncriticalit.com" href="/mc/compose?to=rco@missioncriticalit.com">rco@missioncriticalit.com</a><br>>     </mc/compose?to=<a ymailto="mailto:rco@missioncriticalit.com" href="/mc/compose?to=rco@missioncriticalit.com">rco@missioncriticalit.com</a>>>/* wrote:<br>>      ><br>>      ><br>>      > From: Raphael Collet <<a ymailto="mailto:rco@missioncriticalit.com" href="/mc/compose?to=rco@missioncriticalit.com">rco@missioncriticalit.com</a><br>>     </mc/compose?to=<a
 ymailto="mailto:rco@missioncriticalit.com" href="/mc/compose?to=rco@missioncriticalit.com">rco@missioncriticalit.com</a>>><br>>      > Subject: Re: [mercury-users] Converting List To List Of Tuples<br>>      > To: <a ymailto="mailto:mercury-users@csse.unimelb.edu.au" href="/mc/compose?to=mercury-users@csse.unimelb.edu.au">mercury-users@csse.unimelb.edu.au</a><br>>     </mc/compose?to=<a ymailto="mailto:mercury-users@csse.unimelb.edu.au" href="/mc/compose?to=mercury-users@csse.unimelb.edu.au">mercury-users@csse.unimelb.edu.au</a>><br>>      > Cc: "<a ymailto="mailto:win1for@yahoo.com" href="/mc/compose?to=win1for@yahoo.com">win1for@yahoo.com</a> </mc/compose?to=<a ymailto="mailto:win1for@yahoo.com" href="/mc/compose?to=win1for@yahoo.com">win1for@yahoo.com</a>>"<br>>     <<a ymailto="mailto:win1for@yahoo.com"
 href="/mc/compose?to=win1for@yahoo.com">win1for@yahoo.com</a> </mc/compose?to=<a ymailto="mailto:win1for@yahoo.com" href="/mc/compose?to=win1for@yahoo.com">win1for@yahoo.com</a>>><br>>      > Date: Friday, December 17, 2010, 3:44 AM<br>>      ><br>>      > Dear user,<br>>      ><br>>      > I suggest you to have a look at the modules map, pair and assoc_list.<br>>      > With a map, you will be able to build the frequency map. Start<br>>     from an<br>>      > empty map, then increment the frequency of each character you find in<br>>      > the string. The predicate string.foldl/4 is useful to iterate<br>>     over all<br>>      > the characters of the string.<br>>      ><br>>   
   > To convert the map into a list of pairs, you may simply use<br>>      ><br>>      > ListOfPairs = map.to_assoc_list(FrequencyMap)<br>>      ><br>>      > In your case, the result will be of type list(pair(char, int)). Note<br>>      > that the pair is not strictly a tuple, but a more specific type for a<br>>      > tuple of exactly two elements.<br>>      ><br>>      > The last point you need is to sort the list with list.sort/3. Simply<br>>      > define a comparison predicate to compare two values of type<br>>     pair(char,<br>>      > int): order them by their second components. Something like:<br>>      ><br>>      > :- pred compare_freq(pair(K, int), pair(K, int),
 comparison_result).<br>>      > :- mode compare_freq(in, in, out) is det.<br>>      ><br>>      > compare_freq(X1 - F1, X2 - F2, Result) :- compare(Result, F1, F2).<br>>      ><br>>      > The predicate compare/3 and its associated types is defined in the<br>>      > module builtin.<br>>      ><br>>      ><br>>      > I hope this will help you.<br>>      ><br>>      > Cheers,<br>>      > Raphael<br>>      ><br>>      ><br>>      > On 12/17/2010 05:22 AM, <a ymailto="mailto:win1for@yahoo.com" href="/mc/compose?to=win1for@yahoo.com">win1for@yahoo.com</a><br>>     </mc/compose?to=<a ymailto="mailto:win1for@yahoo.com"
 href="/mc/compose?to=win1for@yahoo.com">win1for@yahoo.com</a>><br>>      > </mc/compose?to=<a ymailto="mailto:win1for@yahoo.com" href="/mc/compose?to=win1for@yahoo.com">win1for@yahoo.com</a><br>>     </mc/compose?to=<a ymailto="mailto:win1for@yahoo.com" href="/mc/compose?to=win1for@yahoo.com">win1for@yahoo.com</a>>> wrote:<br>>      > > Hello,<br>>      > ><br>>      > ><br>>      > > I am just a total beginner in mercury and finding it hard to<br>>      > solve this<br>>      > > problem. I want to convert a list to a list of tupples sorted from<br>>      > > smaller to higher frequenties. Eg:<br>>      > ><br>>      > > |string.to_char_list("this is a test") becomes<br>> 
     > ><br>>      > > [{'a', 1}, {'e', 1}, {'h', 1}, {'i', 2}, {' ', 3}, {'s', 3},<br>>      > {'t', 3}]<br>>      > ><br>>      > > OR<br>>      > ><br>>      > > [3,2,1,2,1,1,2] becomes<br>>      > ><br>>      > > [{3, 1}, {1, 3}, {2, 3}]<br>>      > > |<br>>      > ><br>>      > > You can see that the list of tuples are sorted from smaller to<br>>     higher<br>>      > > frequenties.<br>>      > ><br>>      > > I am asking if someone can help me to solve this or point me to a<br>>      > > tutorial where i can find more tips to do it.<br>>   
   > ><br>>      > ><br>>      > > Thanks for your reply.<br>>      > ><br>>      > ><br>>      ><br>>      ><br>>     --------------------------------------------------------------------------<br>>      > mercury-users mailing list<br>>      > Post messages to: <a ymailto="mailto:mercury-users@csse.unimelb.edu.au" href="/mc/compose?to=mercury-users@csse.unimelb.edu.au">mercury-users@csse.unimelb.edu.au</a><br>>     </mc/compose?to=<a ymailto="mailto:mercury-users@csse.unimelb.edu.au" href="/mc/compose?to=mercury-users@csse.unimelb.edu.au">mercury-users@csse.unimelb.edu.au</a>><br>>      > </mc/compose?to=<a ymailto="mailto:mercury-users@csse.unimelb.edu.au"
 href="/mc/compose?to=mercury-users@csse.unimelb.edu.au">mercury-users@csse.unimelb.edu.au</a><br>>     </mc/compose?to=<a ymailto="mailto:mercury-users@csse.unimelb.edu.au" href="/mc/compose?to=mercury-users@csse.unimelb.edu.au">mercury-users@csse.unimelb.edu.au</a>>><br>>      > Administrative Queries: <a ymailto="mailto:owner-mercury-users@csse.unimelb.edu.au" href="/mc/compose?to=owner-mercury-users@csse.unimelb.edu.au">owner-mercury-users@csse.unimelb.edu.au</a><br>>     </mc/compose?to=<a ymailto="mailto:owner-mercury-users@csse.unimelb.edu.au" href="/mc/compose?to=owner-mercury-users@csse.unimelb.edu.au">owner-mercury-users@csse.unimelb.edu.au</a>><br>>      > </mc/compose?to=<a ymailto="mailto:owner-mercury-users@csse.unimelb.edu.au"
 href="/mc/compose?to=owner-mercury-users@csse.unimelb.edu.au">owner-mercury-users@csse.unimelb.edu.au</a><br>>     </mc/compose?to=<a ymailto="mailto:owner-mercury-users@csse.unimelb.edu.au" href="/mc/compose?to=owner-mercury-users@csse.unimelb.edu.au">owner-mercury-users@csse.unimelb.edu.au</a>>><br>>      > Subscriptions: <a ymailto="mailto:mercury-users-request@csse.unimelb.edu.au" href="/mc/compose?to=mercury-users-request@csse.unimelb.edu.au">mercury-users-request@csse.unimelb.edu.au</a><br>>     </mc/compose?to=<a ymailto="mailto:mercury-users-request@csse.unimelb.edu.au" href="/mc/compose?to=mercury-users-request@csse.unimelb.edu.au">mercury-users-request@csse.unimelb.edu.au</a>><br>>      > </mc/compose?to=<a ymailto="mailto:mercury-users-request@csse.unimelb.edu.au"
 href="/mc/compose?to=mercury-users-request@csse.unimelb.edu.au">mercury-users-request@csse.unimelb.edu.au</a><br>>     </mc/compose?to=<a ymailto="mailto:mercury-users-request@csse.unimelb.edu.au" href="/mc/compose?to=mercury-users-request@csse.unimelb.edu.au">mercury-users-request@csse.unimelb.edu.au</a>>><br>>      ><br>>     --------------------------------------------------------------------------<br>>      ><br>>      ><br>><br>><br><br></div></blockquote></td></tr></table><br>