
As an excercise, I have created a small module defining a data structure which is intended to be an efficient map from sequences (possibly very long ones) to objects of some other type. The source code is below for anyone interested. Three questions. Hopefully they aren't too naive: 1) Can anyone point me toward whatever standard modules would be most appropriate for timing this (or measuring other kinds of performance) against the naive Data.Map? 2) Any suggestions on how to improve the module itself are more than welcome. 3) In a language like this with meaningful white-space, how do people keep their lines of source code from getting too wide? There is one line below that stretched beyond the desired 80 characters and, in general, I seem to have a hard time avoiding this. Thanks for your help. -- mac -- A map from [a] to p module SequenceMap (SequenceMap, empty, insert, lookup, test) where import Prelude hiding (lookup) import qualified Data.Map import Data.Maybe data Ord a => SequenceMap a p = SequenceMap (Maybe p) (Data.Map.Map a (SequenceMap a p)) empty :: Ord a => SequenceMap a p empty = SequenceMap Nothing Data.Map.empty lookup :: Ord a => [a] -> SequenceMap a p -> Maybe p lookup [] (SequenceMap p m) = p lookup (a:as) (SequenceMap p m) = let msm = Data.Map.lookup a m in case msm of Nothing -> Nothing Just sm -> lookup as sm insert :: Ord a => [a] -> p -> SequenceMap a p -> SequenceMap a p insert [] p (SequenceMap oldP othrs) = SequenceMap (Just p) othrs insert (a:as) p (SequenceMap notP othrs) = let msm = Data.Map.lookup a othrs in case msm of Nothing -> SequenceMap notP (Data.Map.insert a (insert as p empty) Data.Map.empty) Just sm -> SequenceMap notP (Data.Map.insert a (insert as p sm) othrs) test :: Bool test = fromMaybe False (lookup "qwerty" (insert "qwerty" True empty))

Hi Matthew A quick one to reduce line width is to import qualified with a single character alias: import qualified Data.Map as M Then the longest line becomes: Nothing -> SequenceMap notP (M.insert a (insert as p empty) M.empty) ... saving 2x7 characters in this case. As for performance always compile (never use GHCi) and always use -O2 otherwise rewrite-rules and other optimizations will not be used by GHC. When you run the executable, get the runtime system to print its stats: ./MyProg +RTS -sstderr -RTS ... This will print memory usage, garbage collection and timing stats. For serious work there is also the Criterion bench-marking package. Best wishes Stephen

On Sat, Jun 19, 2010 at 09:17:02AM -0400, matthew coolbeth wrote:
2) Any suggestions on how to improve the module itself are more than welcome.
You seem to be implementing a Trie. There are many Trie-implementing packages on Hackage, such as: http://hackage.haskell.org/package/TrieMap http://hackage.haskell.org/package/bytestring-trie http://hackage.haskell.org/package/data-inttrie http://hackage.haskell.org/package/list-tries http://hackage.haskell.org/package/gmap Probably you could use one of those. In particular, Patricia tries should be useful for you if you have few long sequences. Cheers, -- Felipe.

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 6/19/10 09:17 , matthew coolbeth wrote:
3) In a language like this with meaningful white-space, how do people keep their lines of source code from getting too wide? There is one line below that stretched beyond the desired 80 characters and, in general, I seem to have a hard time avoiding this.
The same significant whitespace:
foo = bar firstLongArgument secondLongArgumentWithMoreDoodads
- -- brandon s. allbery [linux,solaris,freebsd,perl] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkwc57MACgkQIn7hlCsL25X/KACgjv1ETxl5QHUwMapjnFTP3hyp uG0AoMEVCDa1ar60e8kfaXcybRSXH6du =tlLU -----END PGP SIGNATURE-----
participants (4)
-
Brandon S Allbery KF8NH
-
Felipe Lessa
-
matthew coolbeth
-
Stephen Tetley