你好,我正在使用Haskellgloss创建一个二进制树的图片并模拟值的插入。
我能够创建一个draw
函数来创建树的图片,它工作得很好。
问题出在创建模拟上。我有一个列表,其中包含在树上每次插入值后的所有树,并希望update
-函数在过去的一段时间内拾取列表中i
位置的树,并不断切换列表中i
位置的每棵树生成的图片。
有办法做到这一点吗?
drawTree::(Show a)=>BTree a->Picture
updateTree :: ViewPort->Float->[BTree a]->[BTree a]
updateTree _ dt list=[list!!toInt dt]
main::IO()
main = do
--receives all values to be inserted
values <- getLine
let list = read values :: [Int]
--temp is a list that stores all the tree generated in each insertion
let temp =insertValues list Leaf
--stores the tree contained in the last inserction
let tree = last temp
--stores the tree after the first insertion
let fir=first temp
simulate window background fps [fir] drawTree updateTree
simulate
的思想是,每次调用updateTree
时,都会将模拟移动一步。您的模拟状态不仅仅是树,它还是要放入树中的项目列表。float参数是进入模拟的时间,因此如果需要,可以设置事物移动的动画。然而,对于第一次传递,您可能只希望值在树中每帧显示一个。
所以你想要这样的东西:
type SimState a = ([a], BTree a) -- Or maybe "data" if you prefer.
drawTree :: SimState a -> Picture
updateTree :: ViewPort -> Float -> SimState a -> SimState a
updateTree _ _ ([], t) = ([], t) -- Static once everything is finished.
updateTree _ _ (x:xs, t) = (xs, addNewBtreeNode t x)
这将把项目x
从列表移动到模拟的每一帧的树中。