<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body 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<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;"
cite="mid:CAFTVGQZFmGt8xw7tmpUArgJcWZHk+xaHFHHiohZn9_erJkjksw@mail.gmail.com"
type="cite"><!--[if !IE]><DIV style="border-left: 2px solid #330033; border-right: 2px solid #330033; padding: 0px 15px; margin: 2px 0px;"><![endif]--><span
style="color:#000000;" class="headerSpan">
<div class="moz-cite-prefix">-------- Original Message --------<br>
Subject: Re: [Haskell-beginners] Adapting code from an
imperative loop<br>
From: Matt Williams <a class="moz-txt-link-rfc2396E" href="mailto:matt.williams45.mw@gmail.com"><matt.williams45.mw@gmail.com></a><br>
To: The Haskell-Beginners Mailing List - Discussion of
primarily beginner-level topics related to Haskell
<a class="moz-txt-link-rfc2396E" href="mailto:beginners@haskell.org"><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
moz-do-not-send="true" href="mailto:bob@redivi.com"><a class="moz-txt-link-abbreviated" href="mailto:bob@redivi.com">bob@redivi.com</a></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 moz-do-not-send="true"
href="mailto: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 moz-do-not-send="true"
href="https://hackage.haskell.org/package/hashmap-1.0.0.2/docs/Data-Hashable.html#t:Hashable"
target="_blank">Hashable</a> k, <a
moz-do-not-send="true"
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
moz-do-not-send="true"
href="https://hackage.haskell.org/package/hashmap-1.0.0.2/docs/Data-HashMap.html#t:HashMap"
target="_blank">HashMap</a> k a -> <a
moz-do-not-send="true"
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 moz-do-not-send="true" href="mailto:Beginners@haskell.org"
target="_blank">Beginners@haskell.org</a><br>
<a moz-do-not-send="true"
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 class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
Beginners mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Beginners@haskell.org">Beginners@haskell.org</a>
<a class="moz-txt-link-freetext" href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a>
</pre>
<!--[if !IE]></DIV><![endif]--></blockquote>
<br>
</body>
</html>