Magic squares of odd order: Difference between revisions

Content added Content deleted
m (J: interesting alternate approach)
Line 1,741: Line 1,741:
| otherwise = []
| otherwise = []


-- TEST ---------------------------------------------------
-------------------------- TEST ---------------------------
main :: IO ()
main :: IO ()
main = mapM_ putStrLn $ (showSquare . magicSquare) <$> [3, 5, 7]
main = mapM_ putStrLn $ showSquare . magicSquare <$> [3, 5, 7]


-- GENERIC ------------------------------------------------
------------------------- GENERIC -------------------------
applyN :: Int -> (a -> a) -> a -> a
applyN :: Int -> (a -> a) -> a -> a
applyN n f = foldr (.) id (replicate n f)
applyN n f = foldr (.) id (replicate n f)
Line 1,770: Line 1,770:
showSquare rows =
showSquare rows =
let srows = fmap show <$> rows
let srows = fmap show <$> rows
w = 1 + maximum (length <$> concat srows)
w = succ $ maximum (length <$> concat srows)
in unlines $ concatMap (justifyRight w ' ') <$> srows</lang>
in unlines $ fmap (justifyRight w ' ' =<<) srows</lang>
{{Out}}
{{Out}}
<pre> 8 1 6
<pre> 8 1 6