Order disjoint list items: Difference between revisions
m
→{{header|Haskell}}: ( Reordering a little to foreground top level )
m (→{{header|Haskell}}: ( inlining )) |
m (→{{header|Haskell}}: ( Reordering a little to foreground top level )) |
||
Line 725:
import Data.List (delete, transpose)
import Data.Text hiding (concat, zipWith, foldl, transpose, maximum)
disjointOrder :: Eq a => [a] -> [a] -> [a]
disjointOrder m n =
let▼
segments :: Eq a => [a] -> [a] -> [[a]]▼
segments m n =▼
let▼
split (ms, ns, acc) x ▼
| elem x ns = (ms ++ [acc], delete x ns, []) ▼
| otherwise = (ms, ns, acc ++ [x])▼
(_m, _, _acc) = foldl split ([], n, []) m▼
in _m ++ [_acc]▼
ms = segments m n▼
ns = ((\x -> [x]) <$> n) ++ [[]]▼
▲ in concat $ zipWith (\x y -> x ++ y) ms ns
▲ ms = segments m n
▲ ns = ((\x -> [x]) <$> n) ++ [[]]
▲ segments :: Eq a => [a] -> [a] -> [[a]]
▲ segments m n =
▲ let
▲ split (ms, ns, acc) x
▲ | elem x ns = (ms ++ [acc], delete x ns, [])
▲ | otherwise = (ms, ns, acc ++ [x])
▲ (_m, _, _acc) = foldl split ([], n, []) m
▲ in _m ++ [_acc]
-- TEST -----------------------------------------------------------
tests :: [(Text, Text)]
tests = (\(a, b) -> (pack a, pack b)) <$>
Line 755 ⟶ 756:
("A B","B A"),
("A B B A","B A")]
table :: Text -> [[Text]] -> Text
table delim rows = unlines $ (\r -> (intercalate delim r))
Line 761 ⟶ 762:
let width = (length $ maximum col)
in (justifyLeft width ' ') <$> col) <$> transpose rows)
main :: IO ()
main = putStr $ unpack $ table (pack " -> ") $
|