我在Haskell中有以下的代码:
move :: Camera -> (Double, Double, Double) -> Camera
move camera (xt, yt, zt) = camera { cPosition = (x + xt, y + yt, z + zt) }
where (x, y, z) = cPosition camera
moveForward :: Camera -> Camera
moveForward camera = move camera (-1 * sin ya, 0, -1 * cos ya)
where (_, ya, _) = cRotation camera
moveBackward :: Camera -> Camera
moveBackward camera = move camera (sin ya, 0, cos ya)
where (_, ya, _) = cRotation camera
您会注意到moveForward
和moveBackward
函数具有相同的where
语句。是否有方法可以消除这种重复?我有许多具有相同where
子句的函数(读:不止两个)。
我希望不用将其作为另一个参数传递 - 因为它永远不会改变。它将始终是cRotation
。
(Double, Double, Double)
转换到专门的东西,比如Data.Vect
? - leftaroundaboutData.Vect
,谢谢! - sdasdadas