module Util where import Data.List (transpose) import System.Random -- | Square. square :: Int -> Int square x = x * x -- | Make pair. pair :: a -> b -> (a, b) pair a b = (a, b) -- | Replace i-th element of list to v. replace :: [a] -> Int -> a -> [a] replace ls i v = take i ls ++ [v] ++ drop (i + 1) ls -- | Remove i-th element of list. remove :: Int -> [a] -> [a] remove i = (\(xs, ys) -> xs ++ tail ys) . splitAt i -- | Rotate 2-D list clock-wise. rotate :: Int -> [[a]] -> [[a]] rotate 0 xss = xss rotate (n + 1) xss = rotate n $ transpose $ reverse xss -- | Map 2-D list with index. idxmap2 :: ((Int, Int) -> a -> b) -> [[a]] -> [[b]] idxmap2 f = zipWith (\iy -> zipWith (\ix c -> f (ix,iy) c) [0..]) [0..] -- |Random integer number: 0..n-1 randN :: Int -> IO Int randN n = getStdRandom (randomR (0, n - 1))