3 ms·
FWIW. (Haskell doesn't have a convenient built-in function for printing arrays. Guilty as charged!) size = 10 main = do -- Initialise the
by tome 9y ago
FWIW. (Haskell doesn't have a convenient built-in function for printing arrays. Guilty as charged!)
size = 10
main = do
-- Initialise the array to 0
arr <- newArray ((1,1), (size, size)) 0
let _ = arr :: IOArray (Int, Int) Int
-- Set the diagonal to 1
forM_ [1..size] $ \i -> writeArray arr (i,i) 1
-- Print the array
forM_ [1..size] $ \i -> do
forM_ [1..size] $ \j -> do
a <- readArray arr (i, j)
putChar (if a == 0 then '0' else '1')
when (j == size) (putChar '\n')
- Ajedi32 9y agoThanks, that looks much nicer and more in-line with what I'd expect from an example intended to demonstrate how easy a simple task is in a specific language. As an aside, Ruby doesn't have a built-in function for printing arrays either (unless you count .inspect or .to_s, which let you print _anything_). The code I gave formatted the array as a string using some simple data manipulation functions before printing it; it was intended to match the output of the program you provided.