Four sides of square: Difference between revisions

→‎{{header|Haskell}}: Added a variant expressed in terms of Data.Matrix
(Added 11l)
(→‎{{header|Haskell}}: Added a variant expressed in terms of Data.Matrix)
Line 425:
putStrLn $ intercalate "\n\n" $ map square sizes</syntaxhighlight>
{{out}}
<pre>$ four_sides 0 1 2 3 4 5
<pre>
$ four_sides 0 1 2 3 4 5
 
 
Line 447 ⟶ 446:
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1</pre>
 
</pre>
 
Or, expressed in terms of Data.Matrix:
<syntaxhighlight lang="haskell">import Data.Matrix
 
------------------ FOUR SIDES OF A SQUARE ----------------
 
fourSides :: Int -> Matrix Int
fourSides n = matrix n n go
where
go (i, j)
| or ([(== i), (== j)] <*> [1, n]) = 1
| otherwise = 0
 
 
--------------------------- TEST -------------------------
main :: IO ()
main = mapM_ print $ fourSides <$> [1 .. 5]</syntaxhighlight>
{{Out}}
<pre>┌ ┐
│ 1 │
└ ┘
┌ ┐
│ 1 1 │
│ 1 1 │
└ ┘
┌ ┐
│ 1 1 1 │
│ 1 0 1 │
│ 1 1 1 │
└ ┘
┌ ┐
│ 1 1 1 1 │
│ 1 0 0 1 │
│ 1 0 0 1 │
│ 1 1 1 1 │
└ ┘
┌ ┐
│ 1 1 1 1 1 │
│ 1 0 0 0 1 │
│ 1 0 0 0 1 │
│ 1 0 0 0 1 │
│ 1 1 1 1 1 │
└ ┘</pre>
 
=={{header|J}}==
9,655

edits