Skip to content

Instantly share code, notes, and snippets.

@nushio3
Last active August 29, 2015 14:02
Show Gist options
  • Save nushio3/8ddf5dc78e6b1c7f118b to your computer and use it in GitHub Desktop.
Save nushio3/8ddf5dc78e6b1c7f118b to your computer and use it in GitHub Desktop.
A pangram solver: an example for set-cover package by Henning. http://hackage.haskell.org/package/set-cover
{- |
Choose a set of words so that each alphabet is contained exactly once.
<http://en.wikipedia.org/wiki/Pangram>
This example illustrates the mose
-}
module Main where
import Control.Monad
import qualified Data.Set as Set
import qualified Math.SetCover.Exact as ESC
-- | 1. If you need no helper functions and type synonyms, go directry to step 4.
-- Otherwise, let's be detailed and define the synonyms.
type X = Char -- ^ Type of the element of the set to be covered
type Label = String -- ^ Type of the label given to each subset
-- | 2. Define the customized 'Assign' type synonym, that contains the
-- problem-specific label type and the representation of the set chosen for this problem.
type Assign = ESC.Assign Label (Set.Set X)
-- | 3. Write a helper function that creates a value of type 'Assign'.
assign :: String -> Assign
assign str = ESC.assign str $ Set.fromList str
-- | 4. Define the list of candidate subsets.
-- The set to be covered is implicitly given as the union of all assigns.
assigns :: [Assign]
assigns = map assign
["a", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog",
"cwm", "fjord", "bank", "glyphs", "vext", "quiz", "veg", "balks", "nth", "pyx"]
-- | 5. Write a pretty printer function for a solution.
pprint :: [Label] -> IO ()
pprint strs = putStrLn $ unwords strs
-- | 6. The function @partitions :: [Assign] -> [[label]]@
-- takes the list of the subsets, and returns all solutions.
main :: IO ()
main =
mapM_ pprint $ ESC.partitions assigns
{- $ runhaskell example/Pangram.hs
vext glyphs bank fjord quiz cwm
pyx nth veg balks fjord quiz cwm
Note that 'partitions' searches for the exact subsets,
while the famous "quick brown fox ..." sentence contains many duplicate alphabets.
λ> sort "a quick brown fox jumps over the lazy dog"
" aabcdeefghijklmnoooopqrrstuuvwxyz"
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment