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 --------------------------- |
||
main :: IO () |
main :: IO () |
||
main = mapM_ putStrLn $ |
main = mapM_ putStrLn $ showSquare . magicSquare <$> [3, 5, 7] |
||
-- |
------------------------- 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 = |
w = succ $ maximum (length <$> concat srows) |
||
in unlines $ |
in unlines $ fmap (justifyRight w ' ' =<<) srows</lang> |
||
{{Out}} |
{{Out}} |
||
<pre> 8 1 6 |
<pre> 8 1 6 |