1

您好,我正在使用 Haskell Gloss创建二叉树图片并模拟值的插入。

我能够创建一个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
4

1 回答 1

0

的想法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列表中的项目移动到模拟的每一帧的树中。

于 2021-03-30T11:19:36.187 回答