Haskell-Bool表达式,期望时不进行评估

问题描述 投票:-1回答:1

我正在实现一个简单的wack-a-mole游戏以及用于检查的功能当前是否有痣从地面弹出和/或被撞我已解决的问题,但我不知道为什么原件不起作用。

这里是功能:

--
-- update a single mole. this will be a mappable function with current time and mouse pos as the partially
-- applied args of the function mapped over the list of moles
--
updateMole :: Point -> Double -> Mole -> Mole
updateMole mousePos time mole@(mtype, pos, uptime, up, wasHit) = 
  (mtype, pos, uptime, isUp, gotHit)
  where
    upDuration = mDurationUp !! (fromEnum mtype)
    isUp = if (time > uptime) && (time<(uptime+upDuration)) 
           then True
           else False  
    -- mole is hit if mouse click within bounding box *and* mole is up.
    gotHit = if isUp==True && wasHit==False
             then
                (  ((fst mousePos) > (fst topCorner)) && ((fst mousePos) < (fst botCorner)) &&
                    ((snd mousePos) < (snd topCorner)) && ((snd mousePos) > (snd botCorner))  )
             else 
                wasHit       
    topCorner = ((fst (holeLocations !! pos)) + (fst hdTopLeft), (snd (holeLocations !! pos)) + (snd hdTopLeft))
    botCorner = ((fst (holeLocations !! pos)) + (fst hdBotRight), (snd (holeLocations !! pos)) + (snd hdBotRight))

gotHit永远不会成为真实。

解决方法已更改:

gotHit = if isUp==True && wasHit==False

to

gotHit = if up==True && wasHit==False

我不明白为什么第一种方法是个问题。不应该对“ isUp”进行评估什么时候需要?您可能可以想象信息过载和完全切线信息尝试搜索“ haskell Bool未在where子句中评估”之类的内容时]

如果该程序的整个上下文实际上在这里很重要,则位于:https://code.world/haskell#PK2k3jAAQLehe540gT0JV1g

haskell boolean lazy-evaluation evaluation
1个回答
0
投票

问题最终与评估顺序,懒惰评估无关或其中任何一个。

事实上,游戏中的两个不同事件正在映射到该功能,并且本质上相互干扰。

     TimePassing x      -> let newMoles = map (updateMole (-1000,0) (clock/100)) moles 
                           in World (clock+1) lvl newMoles msg score 
     PointerPress (x,y) -> let checkedMoles = map (updateMole (x,y) clock) moles
                           in World clock lvl checkedMoles msg score 

修复是我所说的,或者更好的解决方法可能是具有两个单独的功能-一个用于处理时间间隔,另一个用于处理指针事件。

© www.soinside.com 2019 - 2024. All rights reserved.