Fairshare between two and more: Difference between revisions

m
Line 753:
 
=={{header|Haskell}}==
<lang haskell>import Data.ListBool (intercalate, unfoldrbool)
import Data.List (intercalate, unfoldr)
import Data.Tuple (swap)
 
import Data.Bool (bool)
------------- FAIR SHARE BETWEEN TWO AND MORE ------------
 
thueMorse :: Int -> [Int]
Line 763 ⟶ 765:
baseDigitsSumModBase base n =
mod
( sum $
unfoldr
(unfoldr ((bool Nothing . Just . swap . flip quotRem base) <*> (0 <)) n))
( bool Nothing
. Just
. swap
(unfoldr ((bool Nothing . Just . swap . flip quotRem base) <*> (0 <)) n))
<*> (0 <)
)
n
show)
base
 
--------------------------- TEST ---------------------------
main :: IO ()
main =
putStrLn $
fTable
( "First 25 fairshare terms for a given number of players:\n"
<> "for a given number of players:\n"
show
)
(('[' :) . (++ " ]") . intercalate "," . fmap (justifyRight 2 ' ' . show))
(take 25 . thueMorse)show
( ('[' :) . (++<> " ]") . intercalate "," . fmap (justifyRight 2 ' ' . show))
[2, 3, 5, 11]
. fmap (show)
)
(take 25 . thueMorse)
[2, 3, 5, 11]
 
------------------------- DISPLAY --------------------------
fTable ::
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
String ->
(a -> String) ->
(b -> String) ->
(a -> b) ->
[a] ->
String
fTable s xShow fxShow f xs =
unlines $
s :
fmap
fmap ( ((++<>) . justifyRight w ' ' . xShow) <*> ((++) " -> " . fxShow . f)) xs
<*> ((" -> " <>) . fxShow . f)
)
xs
where
w = maximum (length . xShow <$> xs)
 
justifyRight :: Int -> aChar -> [a]String -> [a]String
justifyRight n c = (drop . length) <*> (replicate n c ++<>)</lang>
{{Out}}
<pre>First 25 fairshare terms for a given number of players:
 
2 -> [ 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0 ]
3 -> [ 0, 1, 2, 1, 2, 0, 2, 0, 1, 1, 2, 0, 2, 0, 1, 0, 1, 2, 2, 0, 1, 0, 1, 2, 1 ]
5 -> [ 0, 1, 2, 3, 4, 1, 2, 3, 4, 0, 2, 3, 4, 0, 1, 3, 4, 0, 1, 2, 4, 0, 1, 2, 3 ]
11 -> [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 0, 2, 3, 4 ]</pre>
 
=={{header|J}}==
9,659

edits