[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.
>
>e.g,
>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)))
>Map.empty
>strings
>
>this works on your example, and also handles
>
>["Alice", "Bob", "Cindy", "Bob", "Bob", "Dave", "Cindy"] ++ repeat "Tim"
>giving
>
>["Alice","Bob","Cindy","Bob:1","Bob:2","Dave","Cindy:1","Tim","Tim:1","Tim:2",...]
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:
["Alice","Bob:1","Cindy:1","Bob:2","Bob:3","Dave","Cindy:2","Tim:1","Tim:2","Tim:3",...]
Steve Schafer
Fenestra Technologies Corp.
http://www.fenestra.com/
More information about the Haskell-Cafe
mailing list