[Haskell-cafe] Named data type members
Bulat Ziganshin
bulatz at HotPOP.com
Wed Jul 20 01:00:24 EDT 2005
Hello yin,
Wednesday, July 20, 2005, 1:18:25 AM, you wrote:
y> data SomeData =
y> SomeData {
y> int1 :: Int,
y> int2 :: Int
y> }
y> class SomeClass where
y> infix 1 `i_`
y> i_ :: SomeData -> Int -> SomeData
y> infix 1 `_i`
y> _i :: SomeData -> Int -> SomeData
y> instance SomeClass SomeData where
y> (SomeData int1 int2) `i_` i = SomeData (int1 + i) int2
y> (SomeData int1 int2) `_i` i = SomeData int1 (int2 + i)
data SomeData =
SomeData {
int1 :: Int,
int2 :: Int
}
infix 1 `i_`
i_ :: SomeData -> Int -> SomeData
(SomeData int1 int2) `i_` i = SomeData (int1 + i) int2
infix 1 `_i`
_i :: SomeData -> Int -> SomeData
(SomeData int1 int2) `_i` i = SomeData int1 (int2 + i)
y> -- SomeData is now very complex
y> initsomeData :: SomeData
y> initsomeData = do
y> var1 <- init...
y> ...
y> SomeData 0 0 0 1 1 True False (-1) var1 var2 .......
initsomeData :: IO SomeData
initsomeData = do
var1 <- init...
...
return$ SomeData { int1 = 0
, int2 = 0
}
if you will use postional notation, your program can get hard bugs
when you will add new fields to the structure
y> main = do p <- initAplication
y> processEvents p
main = do p <- initAplication
processEvents p
y> -- First Function
y> processEvents :: SomeData -> IO ()
y> processEvents p = do
y> event <- pollEvent
y> case event of
y> Event1 -> do
y> processEvents (SomeData v1 v2 v3 (v4*2) ... v100)
y> Event2 -> do
y> processEvents (SomeData v1 ... False ... v100)
y> Event2' -> do
y> processEvents (SomeData v1 ... True ... v100)
y> EventQuit -> do
y> return ()
y> NoMoreEvents -> do computeNext p
y> _ -> processEvents p
y> where (SomeData v1 v2 v3 v4 v5 ... v100) = p
processEvents :: SomeData -> IO ()
processEvents p = do
event <- pollEvent
case event of
Event1 -> processEvents p{ int3 = (int3 p) * 2 }
Event2 -> processEvents p{ bool1 = False }
EventQuit -> return ()
NoMoreEvents -> computeNext p
_ -> processEvents p
y> -- An time based function
y> computeNextStep = do
y> timeDelta <- getTimeDelta
y> let
y> i1' = compute1 i1 i2 i3 ... i50
y> i2' = compute2 i1 i2 i3 ... i50
y> ...
y> if (condition) then
y> -- need more computing
y> computeNextStep (SomeData u1 u2 ... u50 i1 i2 ... i50)
y> else do
y> p' <- (SomeData u1 u2 ... u50 i1 i2 ... i50)
let p = p {i1=1, i2=2} -- other fields will left unchanged
y> draw p'
y> processEvents p'
y> where (SomeData u1 u2 ... u50 i1 i2 ... i50) = p
y> -- ux - uninteresting (State variables, like left-key-down
y> was last left-key related event: move left)
y> -- ix - interesting for computing
y> -- where x = 1, 2 ... 50
y> ...
y> ...
y> 3b. every funtion needs only a part of the data... can I build for every
y> fintion a class with defined operators to manipulate with the data and then:
y> * pass the variable in func1 known as class A to func2, which is
y> infering a class B?
i don't understand what you mean
--
Best regards,
Bulat mailto:bulatz at HotPOP.com
More information about the Haskell-Cafe
mailing list