<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Hi,<br>
<br>
I have implemented the following code but its not compiling:<br>
<br>
:- module oplossing.<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|Xs]) = map.init(M),<br>
map.to_assoc_list(helper([X|Xs],M,1000)) .<br>
<br>
:- func helper(list(T),map(T),T) = map(T).<br>
helper([X|Xs],Map,VorigElement) = (if X = VorigElement<br>
then map.set(Map, X, map.search(Map,X)+1),helper(Xs, Map,X)<br>
else if X = []<br>
then Map<br>
else map.set(Map, X, 1),helper(Xs, Map,X)<br>
).<br>
<br>
<br>This is the compile errors i get:<br>
<br>
oplossing.m:020: In definition of function `oplossing.helper'/5:<br>
oplossing.m:020: error: undefined type `map'/1.<br>
oplossing.m:020: In definition of function `oplossing.helper'/5:<br>
oplossing.m:020: error: undefined type `map'/1.<br>
oplossing.m:015: Error: clause for predicate `oplossing.,/2'<br>
oplossing.m:015: without preceding `pred' declaration.<br>
<br>
The problem has to do with the declaration of the "map". Can you please help me debugit?<br>
<br>Thank you<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 href="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 href="compose?to=rco@missioncriticalit.com">rco@missioncriticalit.com</a>>/* wrote:<br>><br>><br>> From: Raphael Collet <<a
href="compose?to=rco@missioncriticalit.com">rco@missioncriticalit.com</a>><br>> Subject: Re: [mercury-users] Converting List To List Of Tuples<br>> To: <a href="compose?to=mercury-users@csse.unimelb.edu.au">mercury-users@csse.unimelb.edu.au</a><br>> Cc: "<a href="compose?to=win1for@yahoo.com">win1for@yahoo.com</a>" <<a href="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 href="compose?to=win1for@yahoo.com">win1for@yahoo.com</a><br>> </mc/compose?to=<a href="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 href="compose?to=mercury-users@csse.unimelb.edu.au">mercury-users@csse.unimelb.edu.au</a><br>> </mc/compose?to=<a href="compose?to=mercury-users@csse.unimelb.edu.au">mercury-users@csse.unimelb.edu.au</a>><br>> Administrative Queries: <a
href="compose?to=owner-mercury-users@csse.unimelb.edu.au">owner-mercury-users@csse.unimelb.edu.au</a><br>> </mc/compose?to=<a href="compose?to=owner-mercury-users@csse.unimelb.edu.au">owner-mercury-users@csse.unimelb.edu.au</a>><br>> Subscriptions: <a href="compose?to=mercury-users-request@csse.unimelb.edu.au">mercury-users-request@csse.unimelb.edu.au</a><br>> </mc/compose?to=<a href="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>