<div>Although in this example using Data.Map is overkill, if the alphabet was very large then Data.Map probably would be the way to go. In that case I&#39;d use:</div><div><br></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-size: small;">map head . group . sort</span></font> instead of <font class="Apple-style-span" face="&#39;courier new&#39;, monospace">nub . sort</font></div>

<div><br></div><div>since it&#39;s noticeably quicker for large lists.  This is because nub needs to preserve the order of input, removing redundancies, but you&#39;re sorting it anyway.</div><div><br></div><div>Also, in <font class="Apple-style-span" face="&#39;courier new&#39;, monospace">map (\c -&gt; m Map.! c) s</font><span class="Apple-style-span" style="font-family: courier; "> <span class="Apple-style-span" style="font-family: arial; ">you can use the &#39;section&#39; </span><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">(m Map.!)</font><span class="Apple-style-span" style="font-family: arial; "> instead.  e.g., </span><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">map (m Map.!) s</font></span></div>

<div class="gmail_quote"><br></div><div class="gmail_quote">The Map.! is ugly though.  As you&#39;re only using fromList and (!) from Data.Map, I&#39;d just import those explicitly since they don&#39;t clash with Prelude.  Then you&#39;d have <font class="Apple-style-span" face="&#39;courier new&#39;, monospace">map (m !) s</font></div>

<div class="gmail_quote"><br></div><div class="gmail_quote">Toby.</div><div class="gmail_quote"><br></div><div class="gmail_quote"><br></div><div class="gmail_quote">On Tue, Jun 9, 2009 at 4:59 AM, michael rice <span dir="ltr">&lt;<a href="mailto:nowgate@yahoo.com">nowgate@yahoo.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><table cellspacing="0" cellpadding="0" border="0"><tbody><tr><td valign="top" style="font:inherit"><span style="font-family:courier,monaco,monospace,sans-serif">I wrote a Haskell solution for the Prolog problem stated below. I had written a function SQUISH before discovering that NUB does the same thing. While the solution works, I thought maybe I could apply some functions in the Data.Map module, and so wrote a second version of SERIALIZE, one no longer needing TRANSLATE. Using the Data.Map module is probably overkill for this particular problem, but wanted to familiarize myself with Map type. Suggestions welcome. Prolog code also included below for those interested.</span><br style="font-family:courier,monaco,monospace,sans-serif">

<br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">Michael </span><br style="font-family:courier,monaco,monospace,sans-serif"><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">===========</span><br style="font-family:courier,monaco,monospace,sans-serif"><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">{-</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif"> From &quot;Prolog By Example&quot;, Coelho, Cotta, Problem 42, pg. 63</span><br style="font-family:courier,monaco,monospace,sans-serif"><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">   Verbal statement:</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">   Generate a list of serial numbers for the items of a given
 list,</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">   the members of which are to be numbered in alphabetical order.</span><br style="font-family:courier,monaco,monospace,sans-serif">

<br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">   For example, the list [p,r,o,l,o,g] must generate [4,5,3,2,3,1]</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">-}</span><br style="font-family:courier,monaco,monospace,sans-serif"><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">{-</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">Prelude&gt; :l
 serialize</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">[1 of 1] Compiling Main             ( serialize.hs, interpreted )</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">Ok, modules loaded: Main.</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">*Main&gt; serialize &quot;prolog&quot;</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">[4,5,3,2,3,1]</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">*Main&gt;</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">-} </span><br style="font-family:courier,monaco,monospace,sans-serif"><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">===========Haskell code==========</span><br style="font-family:courier,monaco,monospace,sans-serif">

<br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">import Data.Char</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">import Data.List</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">import Data.Map (Map)</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">import qualified Data.Map as
 Map</span><br style="font-family:courier,monaco,monospace,sans-serif"><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">{-</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">translate :: [Char] -&gt; [(Char,Int)] -&gt; [Int]</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">translate [] _ = []</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">translate (x:xs) m = (fromJust (lookup x m)) : (translate xs m )</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">-}</span><br style="font-family:courier,monaco,monospace,sans-serif">

<br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">{-</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">serialize :: [Char] -&gt; [Int]</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">serialize s = let c = nub $ sort s</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">                  n = [1..(length c)]</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">              in translate s (zip c n)</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">-}</span><br style="font-family:courier,monaco,monospace,sans-serif">

<br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">serialize :: [Char] -&gt; [Int]</span><br style="font-family:courier,monaco,monospace,sans-serif">
<span style="font-family:courier,monaco,monospace,sans-serif">serialize s = let c = nub $ sort s</span><br style="font-family:courier,monaco,monospace,sans-serif">
<span style="font-family:courier,monaco,monospace,sans-serif">                  n = [1..(length c)]</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">                  m = Map.fromList $ zip c n</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">              in map (\c -&gt; m Map.! c) s </span><br style="font-family:courier,monaco,monospace,sans-serif"><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">============Prolog code============</span><br style="font-family:courier,monaco,monospace,sans-serif"><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">serialize(L,R) :- pairlists(L,R,A),arrange(A,T),</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">                  numbered(T,1,N).</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">                                                ?  &lt;- typo?</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">pairlists([X|L],[Y|R],[pair(X,Y)|A]) :- pairlist(L,R,A).</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">pairlists([],[],[]). </span><br style="font-family:courier,monaco,monospace,sans-serif"><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">arrange([X|L],tree(T1,X,T2)) :- partition(L,X,L1,L2),</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">                                arrange(L1,T1),</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">                                arrange(L2,T2).</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">arrange([],_).</span><br style="font-family:courier,monaco,monospace,sans-serif"><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">partition([X|L],X,L1,L2) :- partition(L,X,L1,L2).</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">partition([X|L],Y,[X|L1],L2) :- before(X,Y),</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">                                partition(L,Y,L1,L2).</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">partition([X|L],Y,L1,[X|L2]) :- before(Y,X),</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">                                partition(L,Y,L1,L2).</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">partition([],_,[],[]).</span><br style="font-family:courier,monaco,monospace,sans-serif"><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">before(pair(X1,Y1),pair(X2,Y2)) :- X1&lt;X2.</span><br style="font-family:courier,monaco,monospace,sans-serif">

<br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">numbered(tree(T1,pair(X,N1),T2),N0,N) :- numbered(T1,N0,N1),</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">                                         N2 is N1+1,</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">                                         numbered(T2,N2,N).</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">numbered(void,N,N).</span><br style="font-family:courier,monaco,monospace,sans-serif"><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">============Prolog examples========</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">Execution:</span><br style="font-family:courier,monaco,monospace,sans-serif"><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">?- serialize([p,r,o,l,o,g]).</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">   [4,5,3,2,3,1]</span><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">?- serialize ([i,n,t,.,a,r,t,i,f,i,c,i,a,l]).</span><br style="font-family:courier,monaco,monospace,sans-serif">

<span style="font-family:courier,monaco,monospace,sans-serif">  [5,7,9,1,2,8,9,5,4,5,3,5,2,6]</span><br style="font-family:courier,monaco,monospace,sans-serif"><br></td></tr></tbody></table><br>

      <br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br>