[Haskell-cafe] Decorating a list of strings

Steve Schafer steve at fenestra.com
Thu Nov 2 15:27:58 EST 2006

On Thu, 02 Nov 2006 12:20:55 -0800, you wrote:

>It seems you only need a table of counts of the number of times
>the string has occurred previously, which you can have available
>on the first pass.
>import Data.List
>import Data.Map as Map
>mark strings = snd $ mapAccumL
>(\counts str ->
>(Map.insertWith (+) str 1 counts,
>str++maybe "" (\n -> ':':show n) (Map.lookup str counts)))
>this works on your example, and also handles
>["Alice", "Bob", "Cindy", "Bob", "Bob", "Dave", "Cindy"] ++ repeat "Tim"

No, you missed a part of the second rule: If there are multiple
occurrences of a string, the _first_ occurrence has to be tagged, too,
not just the second through n'th occurrences. The result of the above
would have to be:


Steve Schafer
Fenestra Technologies Corp.

More information about the Haskell-Cafe mailing list