2
0

index.html 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>CodeMirror 2: Haskell mode</title>
  5. <link rel="stylesheet" href="../../lib/codemirror.css">
  6. <script src="../../lib/codemirror.js"></script>
  7. <script src="haskell.js"></script>
  8. <link rel="stylesheet" href="../../theme/elegant.css">
  9. <style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style>
  10. <link rel="stylesheet" href="../../css/docs.css">
  11. </head>
  12. <body>
  13. <h1>CodeMirror 2: Haskell mode</h1>
  14. <form><textarea id="code" name="code">
  15. module UniquePerms (
  16. uniquePerms
  17. )
  18. where
  19. -- | Find all unique permutations of a list where there might be duplicates.
  20. uniquePerms :: (Eq a) => [a] -> [[a]]
  21. uniquePerms = permBag . makeBag
  22. -- | An unordered collection where duplicate values are allowed,
  23. -- but represented with a single value and a count.
  24. type Bag a = [(a, Int)]
  25. makeBag :: (Eq a) => [a] -> Bag a
  26. makeBag [] = []
  27. makeBag (a:as) = mix a $ makeBag as
  28. where
  29. mix a [] = [(a,1)]
  30. mix a (bn@(b,n):bs) | a == b = (b,n+1):bs
  31. | otherwise = bn : mix a bs
  32. permBag :: Bag a -> [[a]]
  33. permBag [] = [[]]
  34. permBag bs = concatMap (\(f,cs) -> map (f:) $ permBag cs) . oneOfEach $ bs
  35. where
  36. oneOfEach [] = []
  37. oneOfEach (an@(a,n):bs) =
  38. let bs' = if n == 1 then bs else (a,n-1):bs
  39. in (a,bs') : mapSnd (an:) (oneOfEach bs)
  40. apSnd f (a,b) = (a, f b)
  41. mapSnd = map . apSnd
  42. </textarea></form>
  43. <script>
  44. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  45. lineNumbers: true,
  46. matchBrackets: true,
  47. theme: "elegant"
  48. });
  49. </script>
  50. <p><strong>MIME types defined:</strong> <code>text/x-haskell</code>.</p>
  51. </body>
  52. </html>