An example of a function of (a -> a -> a) would be (+), which works if your values are numbers (which mirrors your Python-like pseudocode).<br><br>On Friday, June 19, 2015, Matt Williams <<a href="mailto:matt.williams45.mw@gmail.com">matt.williams45.mw@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p dir="ltr">Dear All,</p>
<p dir="ltr">Thanks for your help with this.</p>
<p dir="ltr">I have got the simple version working but now need to use Map.fromListWith, and am having syntax problems.</p>
<p dir="ltr">I found a related question on Stack overflow (here: <a href="http://stackoverflow.com/questions/15514486/haskell-converting-a-list-of-a-b-key-value-pairs-with-possibly-repeated-key" target="_blank">http://</a><a href="http://stackoverflow.com/questions/15514486/haskell-converting-a-list-of-a-b-key-value-pairs-with-possibly-repeated-key" target="_blank">stackoverflow.com</a><a href="http://stackoverflow.com/questions/15514486/haskell-converting-a-list-of-a-b-key-value-pairs-with-possibly-repeated-key" target="_blank">/questions/15514486/</a><a href="http://stackoverflow.com/questions/15514486/haskell-converting-a-list-of-a-b-key-value-pairs-with-possibly-repeated-key" target="_blank">haskell-converting-a-list-</a><a href="http://stackoverflow.com/questions/15514486/haskell-converting-a-list-of-a-b-key-value-pairs-with-possibly-repeated-key" target="_blank">of-</a><a href="http://stackoverflow.com/questions/15514486/haskell-converting-a-list-of-a-b-key-value-pairs-with-possibly-repeated-key" target="_blank">a-b-key-</a><a href="http://stackoverflow.com/questions/15514486/haskell-converting-a-list-of-a-b-key-value-pairs-with-possibly-repeated-key" target="_blank">value-pairs-</a><a href="http://stackoverflow.com/questions/15514486/haskell-converting-a-list-of-a-b-key-value-pairs-with-possibly-repeated-key" target="_blank">with-</a><a href="http://stackoverflow.com/questions/15514486/haskell-converting-a-list-of-a-b-key-value-pairs-with-possibly-repeated-key" target="_blank">possibly-</a><a href="http://stackoverflow.com/questions/15514486/haskell-converting-a-list-of-a-b-key-value-pairs-with-possibly-repeated-key" target="_blank">repeated-key</a> ).</p>
<p dir="ltr">However, I'm having problems understanding the additional function. The signature should be : <br>
(a -> a -> a) -> [(k, a)] -> Map</p>
<p dir="ltr">And so I assume I need to supply a function whose signature is:</p>
<p dir="ltr"> (a -> a -> a) </p>
<p dir="ltr">Is that correct?</p>
<p dir="ltr">Thanks,<br>
Matt</p>
<br><div class="gmail_quote"><div dir="ltr">On Fri, 19 Jun 2015 09:04 Vlatko Basic <<a href="javascript:_e(%7B%7D,'cvml','vlatko.basic@gmail.com');" target="_blank">vlatko.basic@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="background-color:rgb(255,255,255);color:rgb(0,0,0)" bgcolor="#FFFFFF" text="#000000">
To learn, I'd suggest you implement it first with the recursion (a
tip: use a "loop" function in where clause), and than with fold.
Those are important features to understand in Haskell. Try to use
the "higher-level" functions as little as possible until you grasp
the basics (like fold syntax).<br>
<br>
If you just need any solution, fromListWith is fine.<br>
<br>
br,<br>
vlatko</div><div style="background-color:rgb(255,255,255);color:rgb(0,0,0)" bgcolor="#FFFFFF" text="#000000"><br>
<br>
<br>
<blockquote style="border-left:2px solid #330033!important;border-right:2px solid #330033!important;padding:0px 15px 0px 15px;margin:8px 2px" type="cite"><span style="color:#000000">
<div>-------- Original Message --------<br>
Subject: Re: [Haskell-beginners] Adapting code from an
imperative loop<br>
From: Matt Williams <a href="javascript:_e(%7B%7D,'cvml','matt.williams45.mw@gmail.com');" target="_blank"><matt.williams45.mw@gmail.com></a><br>
To: The Haskell-Beginners Mailing List - Discussion of
primarily beginner-level topics related to Haskell
<a href="javascript:_e(%7B%7D,'cvml','beginners@haskell.org');" target="_blank"><beginners@haskell.org></a><br>
Date: 19/06/15 09:05<br>
</div>
<br>
<br>
</span>
<p dir="ltr">I tried doing it as a fold (the pattern of
accumulating a value, where the accumulated value was the
resulting Map), but couldn't manage the syntax.</p>
<p dir="ltr">Have now got it partially working with fromListWith.</p>
<p dir="ltr">Thanks a lot,<br>
Matt</p>
<br>
<div class="gmail_quote">
<div dir="ltr">On Fri, 19 Jun 2015 07:18 Bob Ippolito <<a href="javascript:_e(%7B%7D,'cvml','bob@redivi.com');" target="_blank"></a><a href="javascript:_e(%7B%7D,'cvml','bob@redivi.com');" target="_blank">bob@redivi.com</a>>
wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Friday,
June 19, 2015, Matt Williams <<a href="javascript:_e(%7B%7D,'cvml','matt.williams45.mw@gmail.com');" target="_blank">matt.williams45.mw@gmail.com</a>>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<p dir="ltr">Dear All,</p>
<p dir="ltr">I have been wrestling with this for a while
now.</p>
<p dir="ltr">I have a list of data items, and want to be
able to access them, in a Hash Map, via a short summary of
their characteristics.</p>
<p dir="ltr">In an imperative language this might look like:</p>
<p dir="ltr">myMap = new map()<br>
for elem in myElems:<br>
key = makeKey(elem)<br>
myMap[key] = myMap[key] + elem</p>
<p dir="ltr">I have been trying to do this in Haskell, but
am stuck on how to hand the Map back to itself each time.</p>
<p dir="ltr">I have a function :: elem -> [p,q] to make
the key, but the Map.insert function has the following
signature:</p>
<p dir="ltr">insert :: (<a href="https://hackage.haskell.org/package/hashmap-1.0.0.2/docs/Data-Hashable.html#t:Hashable" target="_blank">Hashable</a> k, <a href="https://hackage.haskell.org/packages/archive/base/4.2.0.2/doc/html/Data-Ord.html#t:Ord" target="_blank">Ord</a> k) => k -> a -> <a href="https://hackage.haskell.org/package/hashmap-1.0.0.2/docs/Data-HashMap.html#t:HashMap" target="_blank">HashMap</a> k a -> <a href="https://hackage.haskell.org/package/hashmap-1.0.0.2/docs/Data-HashMap.html#t:HashMap" target="_blank">HashMap</a> k a</p>
<p dir="ltr">My thought was that I needed to go through the
list of the elems, and at each point add them to the Hash
Map, handing the updated Map onto the next step - but this
is what I cannot write.</p>
</blockquote>
<div><br>
</div>
<div>This is typically done with fromListWith or a combination
of foldl' and insertWith or alter.</div>
<div><br>
</div>
<div>-bob</div>
_______________________________________________<br>
Beginners mailing list<br>
<a href="javascript:_e(%7B%7D,'cvml','Beginners@haskell.org');" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote>
</div>
<br>
<fieldset></fieldset>
<br>
<pre>_______________________________________________
Beginners mailing list
<a href="javascript:_e(%7B%7D,'cvml','Beginners@haskell.org');" target="_blank">Beginners@haskell.org</a>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a>
</pre>
</blockquote>
<br>
</div>
_______________________________________________<br>
Beginners mailing list<br>
<a href="javascript:_e(%7B%7D,'cvml','Beginners@haskell.org');" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div>
</blockquote>