我目前正在通過使用該庫編寫一些簡單的函數來學習鏡頭庫。不幸的是,我對生成的編譯器錯誤感到困惑,所以我正在努力確定爲什麼,在函數dmg
中,前兩個函數編譯正確,但最後一個失敗。如何正確使用Control.Lens格式化函數?
import Control.Lens
type Health = Int
type Damage = Int
data Card = Card {
_health :: Int,
_damage :: Int
} deriving (Show,Eq)
health :: Lens' Card Health
health = lens _health (\card h -> card { _health = h })
damage :: Lens' Card Damage
damage = lens _damage (\card d -> card { _damage = d })
cardDead :: Card -> Bool
cardDead c = c^.health <= 0
duel :: (Card,Card) -> (Card,Card)
duel (c,c2) = ((dmg c c2),(dmg c2 c))
這件事真正的肉。
dmg :: Card -> Card -> Card
dmg myCard otherCard = over health ((-) (otherCard^.damage)) myCard --compiles
dmg myCard otherCard = myCard & health %~ ((-) (otherCard^.damage)) --compiles
dmg myCard otherCard = health %~ ((-) (otherCard^.damage)) myCard --compile error
我的問題分三部分。
爲什麼第三個dmg函數無法編譯?
我該怎麼寫dmg仍然使用
(%~)
運算符,而不是使用(&)
,仍然編譯?什麼是最美麗,最習慣的鏡頭寫作方式
dmg
?
-
僅供參考,這裏是你可能會寫DMG不帶鏡頭的一種方式
dmg myCard otherCard =
let
damageTaken = _damage otherCard
oldHealth = _health myCard
newHealth = oldHealth - damageTaken
in myCard {_health = newHealth}
編輯:供參考,在這裏是我遇到了麻煩與理解錯誤信息(錯誤地寫)行3.
*Main GHC.Arr Control.Applicative Control.Lens> :l Doom.hs
[1 of 1] Compiling Main (Doom.hs, interpreted)
Doom.hs:26:24:
Couldn't match expected type `Card' with actual type `Card -> Card'
In the expression: health %~ ((-) (otherCard ^. damage)) myCard
In an equation for `dmg':
dmg myCard otherCard = health %~ ((-) (otherCard ^. damage)) myCard
Doom.hs:26:51:
Couldn't match type `Health -> Health' with `Int'
Expected type: Getting (Health -> Health) Card (Health -> Health)
Actual type: (Damage -> Const (Health -> Health) Damage)
-> Card -> Const (Health -> Health) Card
In the second argument of `(^.)', namely `damage'
In the first argument of `(-)', namely `(otherCard ^. damage)'
Doom.hs:26:60:
Couldn't match expected type `Health -> Health'
with actual type `Card'
In the second argument of `(-)', namely `myCard'
In the second argument of `(%~)', namely
`((-) (otherCard ^. damage)) myCard'
Failed, modules loaded: none.
Prelude GHC.Arr Control.Applicative Control.Lens>
如果你要談論「混淆編譯錯誤」,你應該真的包括它們。 – Carl
好點。下面的答案非常完美,但後人生病包括我編寫P3的錯誤。 – SolventGren