2
0

index.html 1.8 KB

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