roll_n_dice :: Int -> StateStdGen [Int] roll_n_dice n = replicateM n $ roll_die -- counting tick :: StateInt () tick = do n <- get put $ n+1 return ()
plus_one :: Int -> Int plus_one seed = execState tick seed
plus_n :: Int -> Int -> Int plus_n seed n = execState (replicateM n tick) seed
-- labelling tree dataTree a = Nil | Node a (Tree a) (Tree a) deriving (Eq, Show)
typeTable a = M.Map a Int
number_tree :: (Ord a, Eq a) => Tree a -> State (Table a) (TreeInt) number_treeNil = return Nil number_tree (Node x t1 t2) = do num <- number_node x nt1 <- number_tree t1 nt2 <- number_tree t2 return $ Node num nt1 nt2 where number_node :: (Ord a, Eq a) => a -> State (Table a) Int number_node x = do table <- get caseM.lookup x table of Just index -> return index Nothing -> let size = M.size table table' = M.insert x size table in put table' >> return size
convert_tree :: (Ord a, Eq a) => Tree a -> TreeInt convert_tree t = evalState (number_tree t) M.empty