[Haskell-cafe] Named data type members

yin yin at atom.sk
Tue Jul 19 17:18:25 EDT 2005


Hello,

I've a data type:

    data SomeData =
        SomeData {
            int1 :: Int,
            int2 :: Int
        }
   
    class SomeClass where
       infix 1 `i_`
       i_ :: SomeData -> Int -> SomeData
       infix 1 `_i`
       _i :: SomeData -> Int -> SomeData
   
    instance SomeClass SomeData where
       (SomeData int1 int2) `i_` i = SomeData (int1 + i) int2
       (SomeData int1 int2) `_i` i = SomeData int1 (int2 + i)

1.  Possible optimalizations ...?
2.  There are several data type members in 'SomeData' and I wish to
access them in other way then "(SomeData m1 m2 m3 m4 m5 ... m100) `a` i
= SomeData (m1 + i) m2 m3 ... m100"
3a. The 'SomeData' is something like c structure, which holds all
application data. One instanciated variable is passed in more functions
to each other:

    -- SomeData is now very complex
    initsomeData :: SomeData
    initsomeData = do
        var1 <- init...
        ...
        SomeData 0 0 0 1 1 True False (-1) var1 var2 .......
   
    main = do p <- initAplication
       processEvents p
   
    -- First Function
    processEvents :: SomeData -> IO ()
    processEvents p = do
       event <- pollEvent
       case event of
          Event1 -> do
             processEvents (SomeData v1 v2 v3 (v4*2) ... v100)
          Event2 -> do
             processEvents (SomeData v1 ... False ... v100)
          Event2' -> do
             processEvents (SomeData v1 ... True ... v100)
          EventQuit -> do
             return ()
          NoMoreEvents -> do computeNext p
          _ -> processEvents p
       where (SomeData v1 v2 v3 v4 v5 ... v100) = p
   
    -- An time based function
    computeNextStep = do
        timeDelta <- getTimeDelta
        let
            i1' = compute1 i1 i2 i3 ... i50
            i2' = compute2 i1 i2 i3 ... i50
           ...
       if (condition) then
          -- need more computing
          computeNextStep (SomeData u1 u2 ... u50 i1 i2 ... i50)
       else do
          p' <- (SomeData u1 u2 ... u50 i1 i2 ... i50)
          draw p'
          processEvents p'
       where (SomeData u1 u2 ... u50 i1 i2 ... i50) = p
            -- ux - uninteresting (State variables, like left-key-down
was last left-key related event: move left)
            -- ix - interesting for computing
            -- where x = 1, 2 ... 50
    ...
    ...
3b. every funtion needs only a part of the data... can I build for every
fintion a class with defined operators to manipulate with the data and then:
    * pass the variable in func1 known as class A to func2, which is
infering a class B?

Thank you all...

Matej 'Yin' Gagyi



More information about the Haskell-Cafe mailing list