7 ms·
That's not really a line for line port. Or even block for block since yours is never uninitialized. The original code was designed to basically mimic writing ba
by WarDaft 9y ago
That's not really a line for line port. Or even block for block since yours is never uninitialized. The original code was designed to basically mimic writing bad C in Perl, with an uninitialised array that you then explicitly looped over to initialise to zero, and then modified the diagonal. That sort of thing is difficult on purpose in Haskell.
On the other hand...
main = mapM_ (putStrLn . foldMap show) [bool 0 1 . (==x) <$> [1..size]| x<-[1..size]]
Or
x .* y = replicate y x
main = mapM_ (putStrLn . foldMap show) [0 .* x ++ 1 : 0 .* (size-x)| x<-[0..size-1]]
Or if you really do want mutation shorthand (because you're a crazy person) just build it:
a .// l = mapM_ (uncurry $ writeArray a) l
pickFn f g (b,v) | b = g v | otherwise = f v
main = do
arr <- newArray ((1,1), (size,size)) 0
arr .// [((x,x),1)|x<-[1..size]]
mapM_ (pickFn putStr putStrLn . (((==size).snd) *** show)) $ getAssocs arr
It's not pretty, but it's not supposed to be. Do you know what is pretty? Doing things the right way.
sudo apt-get install haskell-stack
stack setup
stack ghci --package matrix
import Data.Matrix
print $ identity 10
And of course there's similar for Ruby.