Order disjoint list items: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: (Cleaner construction of single element list with (:[]))
(→‎{{header|Haskell}}: minor improvement)
Line 689: Line 689:
</pre>
</pre>
=={{header|Haskell}}==
=={{header|Haskell}}==
<lang Haskell>
<lang Haskell>import Data.List
import Data.List


order::Ord a => [[a]] -> [a]
order::Ord a => [[a]] -> [a]
order [ms,ns] = reverse.fst.foldl yu ([],ls) $ ks
order [ms,ns] = snd.mapAccumL yu ls $ ks
where
where
ks = zip ms [(0::Int)..]
ks = zip ms [(0::Int)..]
ls = zip ns.sort.snd.foldl go (sort ns,[]).sort $ ks
ls = zip ns.sort.snd.foldl go (sort ns,[]).sort $ ks
yu (xs,[]) (x,_) = (x:xs,[])
yu [] (x,_) = ([],x)
yu (xs,ys@((u,v):us)) (x,y) = if v == y then (u:xs,us) else (x:xs,ys)
yu (ys@((u,v):us)) (x,y) = if v == y then (us,u) else (ys,x)
go ([],ys) _ = ([],ys)
go ([],ys) _ = ([],ys)
go ts@(xs@(u:us),ys) (x,y) = if u == x then (us,y:ys) else ts
go ts@(xs@(u:us),ys) (x,y) = if u == x then (us,y:ys) else ts
Line 705: Line 704:
putStrLn $ "M: " ++ ms ++ " | N: " ++ ns ++ " |> " ++ (unwords.order.map words $ ls)
putStrLn $ "M: " ++ ms ++ " | N: " ++ ns ++ " |> " ++ (unwords.order.map words $ ls)
main = mapM_ task [["the cat sat on the mat","mat cat"],["the cat sat on the mat","cat mat"],["A B C A B C A B C","C A C A"],["A B C A B D A B E","E A D A"],["A B","B"],["A B","B A"],["A B B A","B A"]]
main = mapM_ task [["the cat sat on the mat","mat cat"],["the cat sat on the mat","cat mat"],["A B C A B C A B C","C A C A"],["A B C A B D A B E","E A D A"],["A B","B"],["A B","B A"],["A B B A","B A"]]</lang>
</lang>
{{out}}
{{out}}
<pre>
<pre>