[mercury-users] Implementation errors: Converting List To List Of Tuples
win1for at yahoo.com
win1for at yahoo.com
Sat Dec 18 00:45:44 AEDT 2010
Hi,
I have implemented the following code but its not compiling:
:- module oplossing.
:- 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|Xs]) = map.init(M),
map.to_assoc_list(helper([X|Xs],M,1000)) .
:- func helper(list(T),map(T),T) = map(T).
helper([X|Xs],Map,VorigElement) = (if X = VorigElement
then map.set(Map, X, map.search(Map,X)+1),helper(Xs, Map,X)
else if X = []
then Map
else map.set(Map, X, 1),helper(Xs, Map,X)
).
This is the compile errors i get:
oplossing.m:020: In definition of function `oplossing.helper'/5:
oplossing.m:020: error: undefined type `map'/1.
oplossing.m:020: In definition of function `oplossing.helper'/5:
oplossing.m:020: error: undefined type `map'/1.
oplossing.m:015: Error: clause for predicate `oplossing.,/2'
oplossing.m:015: without preceding `pred' declaration.
The problem has to do with the declaration of the "map". Can you please help me debugit?
Thank you
--- On Fri, 12/17/10, Raphael Collet <rco at missioncriticalit.com> wrote:
From: Raphael Collet <rco at missioncriticalit.com>
Subject: Re: [mercury-users] 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, 6:33 AM
Dear anonymous user,
What you ask for looks a lot like a homework... I ain't going to do the
homework for you. Here are a few hints, though.
The function map.init returns an empty map. That's where you should
start from.
The predicate map.set/4 (and the function map.set/3) allows you to set
the value associated to a key in the map. In your case, this could be
the current frequency of a character.
The predicate map.search/3 looks up for a given key, and return the
corresponding value. You can use this to retrieve the frequency of a
character. The typical use-case is:
( map.search(Map, Key, Value) ->
% do something with the value
;
% no value for key, do something else
)
Cheers,
Raphael
On 12/17/2010 11:56 AM, win1for at yahoo.com wrote:
>
> Please am a total beginner can you give me an example of how to use the map.
>
> thanks for your reply
> --- On *Fri, 12/17/10, Raphael Collet /<rco at missioncriticalit.com>/* wrote:
>
>
> From: Raphael Collet <rco at missioncriticalit.com>
> Subject: Re: [mercury-users] 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, 3:44 AM
>
> Dear user,
>
> I suggest you to have a look at the modules map, pair and assoc_list.
> With a map, you will be able to build the frequency map. Start from an
> empty map, then increment the frequency of each character you find in
> the string. The predicate string.foldl/4 is useful to iterate over all
> the characters of the string.
>
> To convert the map into a list of pairs, you may simply use
>
> ListOfPairs = map.to_assoc_list(FrequencyMap)
>
> In your case, the result will be of type list(pair(char, int)). Note
> that the pair is not strictly a tuple, but a more specific type for a
> tuple of exactly two elements.
>
> The last point you need is to sort the list with list.sort/3. Simply
> define a comparison predicate to compare two values of type pair(char,
> int): order them by their second components. Something like:
>
> :- pred compare_freq(pair(K, int), pair(K, int), comparison_result).
> :- mode compare_freq(in, in, out) is det.
>
> compare_freq(X1 - F1, X2 - F2, Result) :- compare(Result, F1, F2).
>
> The predicate compare/3 and its associated types is defined in the
> module builtin.
>
>
> I hope this will help you.
>
> Cheers,
> Raphael
>
>
> On 12/17/2010 05:22 AM, win1for at yahoo.com
> </mc/compose?to=win1for at yahoo.com> wrote:
> > Hello,
> >
> >
> > I am just a total beginner in mercury and finding it hard to
> solve this
> > problem. I want to convert a list to a list of tupples sorted from
> > smaller to higher frequenties. Eg:
> >
> > |string.to_char_list("this is a test") becomes
> >
> > [{'a', 1}, {'e', 1}, {'h', 1}, {'i', 2}, {' ', 3}, {'s', 3},
> {'t', 3}]
> >
> > OR
> >
> > [3,2,1,2,1,1,2] becomes
> >
> > [{3, 1}, {1, 3}, {2, 3}]
> > |
> >
> > You can see that the list of tuples are sorted from smaller to higher
> > frequenties.
> >
> > I am asking if someone can help me to solve this or point me to a
> > tutorial where i can find more tips to do it.
> >
> >
> > Thanks for your reply.
> >
> >
>
> --------------------------------------------------------------------------
> mercury-users mailing list
> Post messages to: mercury-users at csse.unimelb.edu.au
> </mc/compose?to=mercury-users at csse.unimelb.edu.au>
> Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
> </mc/compose?to=owner-mercury-users at csse.unimelb.edu.au>
> Subscriptions: mercury-users-request at csse.unimelb.edu.au
> </mc/compose?to=mercury-users-request at csse.unimelb.edu.au>
> --------------------------------------------------------------------------
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20101217/d3113b29/attachment.html>
More information about the users
mailing list