<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;"><br>Please am a total beginner can you give me an example of how to use the map.<br><br>thanks for your reply<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: mercury-users@csse.unimelb.edu.au<br>Cc: "win1for@yahoo.com" <win1for@yahoo.com><br>Date: Friday, December 17, 2010, 3:44 AM<br><br><div class="plainMail">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 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 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 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> wrote:<br>> Hello,<br>><br>><br>> I am just a total beginner in mercury and finding it hard to 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}, {'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 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>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>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>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>--------------------------------------------------------------------------<br></div></blockquote></td></tr></table><br>