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