Non-transitive dice: Difference between revisions

→‎{{header|Haskell}}: added solution
m (→‎{{header|R}}: Changed variable name.)
(→‎{{header|Haskell}}: added solution)
Line 311:
[[2 2 3 3] [1 3 3 3] [1 1 4 4] [2 2 2 4]]
</pre>
 
=={{header|Haskell}}==
 
<lang haskell>{-# language LambdaCase #-}
import Data.List
import Control.Monad
 
newtype Dice = Dice [Int]
 
instance Show Dice where
show (Dice s) = "(" ++ unwords (show <$> s) ++ ")"
 
instance Eq Dice where
d1 == d2 = d1 `compare` d2 == EQ
instance Ord Dice where
Dice d1 `compare` Dice d2 = (add $ compare <$> d1 <*> d2) `compare` 0
where
add = sum . map (\case {LT -> -1; EQ -> 0; GT -> 1})
 
dices n = Dice <$> (nub $ sort <$> replicateM n [1..n])
 
nonTrans dice = filter (\x -> last x < head x) . go
where
go 0 = []
go 1 = sequence [dice]
go n = do
(a:as) <- go (n-1)
b <- filter (< a) dice
return (b:a:as)</lang>
 
<pre> mapM_ print $ nonTrans (dices 4) 3
[(2 2 2 4),(1 3 3 3),(1 1 4 4)]
[(1 1 4 4),(2 2 2 4),(1 3 3 3)]
[(1 3 3 3),(1 1 4 4),(2 2 2 4)]
 
*Main> mapM_ print $ nonTrans (dices 4) 4
[(2 2 2 4),(2 2 3 3),(1 3 3 3),(1 1 4 4)]
[(1 1 4 4),(2 2 2 4),(2 2 3 3),(1 3 3 3)]
[(2 2 3 3),(1 3 3 3),(1 1 4 4),(2 2 2 4)]
[(1 3 3 3),(1 1 4 4),(2 2 2 4),(2 2 3 3)]
 
*Main> length $ nonTrans (dices 4) 5
0
 
*Main> length $ nonTrans (dices 5) 3
2022</pre>
 
=={{header|Java}}==
Anonymous user