This commit is contained in:
2024-12-09 21:22:46 +01:00
parent 6f5e886851
commit c6b6fe4b82
11 changed files with 328 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
module Common where
data BlockType = Empty | Allocated Int
instance Show BlockType where
show Empty = "(empty)"
show (Allocated blockId) = show blockId
instance Eq BlockType where
(==) Empty Empty = True
(==) (Allocated a) (Allocated b) = True
(==) _ _ = False
type Block = (BlockType, Int)
length :: Block -> Int
length (_, l) = l
parseBlocks :: String -> [Block]
parseBlocks = startWithBlock 0
where
startWithBlock :: Int -> String -> [Block]
startWithBlock _ [] = []
startWithBlock blockId (size:rest)
| size >= '0' && size <= '9' = (Allocated blockId, read [size]) : startWithEmpty (blockId + 1) rest
| otherwise = startWithBlock blockId rest
startWithEmpty :: Int -> String -> [Block]
startWithEmpty _ [] = []
startWithEmpty blockId (size:rest)
| size >= '0' && size <= '9' = (Empty, read [size]) : startWithBlock blockId rest
| otherwise = startWithEmpty blockId rest