Minimum number of cells after, before, above and below NxN squares: Difference between revisions

m
→‎{{header|Haskell}}: Added a variant
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
m (→‎{{header|Haskell}}: Added a variant)
Line 1,449:
│ 0 │
└ ┘</pre>
 
or bypassing 'minimum', to reduce the count of comparisons (same output as above):
 
<syntaxhighlight lang="haskell">import Data.Bifunctor (bimap)
import Data.Bool (bool)
import Data.Matrix (Matrix, matrix)
 
----------- SHORTEST DISTANCES TO EDGE OF MATRIX ---------
 
distancesToEdge :: Int -> Matrix Int
distancesToEdge n = matrix n n (uncurry min . bimap f f)
where
m = quot n 2
f = bool . (-) n <*> pred <*> (<= m)
 
--------------------------- TEST -------------------------
main :: IO ()
main = mapM_ print $ distancesToEdge <$> [10, 9, 2, 1]</syntaxhighlight>
 
=={{header|J}}==
9,655

edits