Skip to content

Instantly share code, notes, and snippets.

type Grid = [String]
validGrid :: Grid -> Bool
validGrid [] = False
validGrid x = allEqual $ map length x
validateGrid :: Grid -> Grid
validateGrid g@(validGrid -> True) = g
validateGrid _ = error "Broken grid!"
--Converts a move into a bitmask
naiveMove :: Int -> Int --Size of the grid
-> Int -> Int --Coordinates of the move
-> Integer --Resulting grid data
naiveMove width height moveX moveY = foldl set 0 $ (moveX, moveY) : neighbors where
set bits (x, y)
| x < 0 || y < 0 || x >= width || y >= height = bits
| otherwise = bits `setBit` ((y * width) + x)
neighbors = [(moveX + i, moveY + j) | i <- [-1..1], j <- [-1..1], abs i + abs j == 1]
--Converts a move into a bitmask
naiveMove :: Int -> Int --Size of the grid
-> Int -> Int --Coordinates of the move
-> Integer --Resulting grid data
naiveMove width height moveX moveY = foldl set 0 $ (moveX, moveY) : neighbors where
set bits (x, y)
| x < 0 || y < 0 || x >= width || y >= height = bits
| otherwise = bits `setBit` ((y * width) + x)
neighbors = [(moveX + i, moveY + j) | i <- [-1..1], j <- [-1..1], abs i + abs j == 1]
--A simple stack-based RPN interpreter
rpnCalc :: String -> Int
rpnCalc expr = if null res then 0 else head res where
res = foldl eval [] expr
eval stack c
| c `elem` ['0'..'9'] = digitToInt c : stack
| otherwise = func c top2 top1 : pop2
where (top1, _) = pop 1 stack
(top2, pop2) = pop 2 stack
--Char to function mappings
mergesort :: Ord a => [a] -> [a]
mergesort [] = []
mergesort [x] = [x]
mergesort (split -> (left, right)) = merge (mergesort left) (mergesort right)
where merge [] rs = rs
merge ls [] = ls
merge (l : ls) (r : rs)
| l < r = l : merge ls (r : rs)
| otherwise = r : merge (l : ls) rs
pacMan :: Int -> [Dependency] -> [Package]
pacMan n deps
| outOfBounds = error "Impossible to resolve"
| Just xs <- try deps = xs ++ top xs
| otherwise = error "Impossible to resolve"
where outOfBounds = any (> n) $ deps >>= (\(x, y) -> [x, y])
try d | null d = Just []
try d = do
let dependedOn = S.fromList $ map snd d
let dependers = S.fromList $ map fst d
pop :: State (Seq Int, Set Int) (Maybe [Int])
pop = do
(x :< xs) <- viewl <$> gets fst
modify $ const xs *** id
return x
pop :: State (Seq Int, Set Int) (Maybe [Int])
pop = do
(x :< xs) <- viewl <$> gets fst
modify $ const xs *** id
return x
No instance for (MonadState
(Seq (Maybe [Int]), Set Int)
(StateT (Seq Int, Set Int) Data.Functor.Identity.Identity))
arising from a use of `gets'
In the second argument of `(<$>)', namely `gets fst'
In a stmt of a 'do' block: (x :< xs) <- viewl <$> gets fst
In the expression:
do { (x :< xs) <- viewl <$> gets fst;
modify $ const xs *** id;
pop :: MaybeT (State BfsState) [Int]
pop = do
(x :< xs) <- viewl <$> gets queue
modify $ \y -> y { queue = xs }
return x
push :: [Int] -> MaybeT (State BfsState) ()
push x = do q <- gets queue
modify $ \y -> y { queue = q |> x }