File: String.hs

package info (click to toggle)
haskell-unordered-containers 0.2.20-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 376 kB
  • sloc: haskell: 4,446; makefile: 6
file content (34 lines) | stat: -rw-r--r-- 1,437 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
-- | Benchmarking utilities.  For example, functions for generating
-- random strings.
module Util.String where

import System.Random (mkStdGen, randomRs)

-- | Generate a number of fixed length strings where the content of
-- the strings are letters in ascending order.
asc :: Int  -- ^ Length of each string
    -> Int  -- ^ Number of strings
    -> [String]
asc strlen num = take num $ iterate (snd . inc) $ replicate strlen 'a'
  where inc [] = (True, [])
        inc (c:cs) = case inc cs of (True, cs') | c == 'z'  -> (True, 'a' : cs')
                                                | otherwise -> (False, succ c : cs')
                                    (False, cs')            -> (False, c : cs')

-- | Generate a number of fixed length strings where the content of
-- the strings are letters in random order.
rnd :: Int  -- ^ Length of each string
    -> Int  -- ^ Number of strings
    -> [String]
rnd strlen num = take num $ split $ randomRs ('a', 'z') $ mkStdGen 1234
  where
    split cs = case splitAt strlen cs of (str, cs') -> str : split cs'

-- | Generate a number of fixed length strings where the content of
-- the strings are letters in random order, different from rnd
rnd' :: Int  -- ^ Length of each string
     -> Int  -- ^ Number of strings
     -> [String]
rnd' strlen num = take num $ split $ randomRs ('a', 'z') $ mkStdGen 5678
  where
    split cs = case splitAt strlen cs of (str, cs') -> str : split cs'