<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Hi Raphael,<br><br> Thank you so much for your reply. I really appreciate it. I have been taught Mercury for only 4 hours and am suppose to do that task. My only problem now is how to begin. Infact i really want to do it myself so that i can learn from it. So i will ask you specific questions to help 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 - (lists of tuples) - The first element in each tuple is an element from the list and the second element is the frequentie of the element in the 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 elements in the list. Meaning that the values will be empty and then 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 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 fit in so that i can understand the procedure and do it. I need the steps so that i can do it. I have good understanding from your replies 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 <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, 6:33 AM<br><br><div class="plainMail">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> wrote:<br>><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 *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, 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 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><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 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>> </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>> 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>> --------------------------------------------------------------------------<br>><br>><br><br></div></blockquote></td></tr></table><br>