代码之家  ›  专栏  ›  技术社区  ›  cibercitizen1

数据类型的别名

  •  2
  • cibercitizen1  · 技术社区  · 6 年前

    我要定义数据类型 Polar 使用极坐标进行计算:

    data Polar = Polar { distance :: Double, angle :: Double }
    

    并具有如下功能 rotate , sum 等等。

    然后,我想有一个“同义词类”(或只是别名)像 TwsTwa (真实风速和真实风向角)如

    data TwsTwa = TwsTwa { tws :: Double, twa :: Double }
    

    不用重新定义函数。

    以下是使用类的最佳解决方案吗? 是矮一点的吗?请注意,我指的是 特斯瓦 定义,而不是编写通用代码的难度 极地的 . 有哪些选择?

    -- .................................................
    -- .................................................
    class PolarFamily tPolar where
      distanceP :: tPolar -> Double
      angleP :: tPolar -> Double
      toMe :: Polar -> tPolar
    
      rotate :: tPolar -> Double -> tPolar
      rotate pol ang =
         toMe $ Polar {distance=distanceP pol, angle=(angleP pol) + ang}
    
      sumP :: tPolar -> tPolar -> tPolar
      sumP p1 p2 =
         toMe $ Polar {distance=(distanceP p1)+(distanceP p2), 
                angle=(angleP p1)+(angleP p2) } -- caution: wrong calculation
    
    -- .................................................
    -- .................................................
    data Polar = Polar { distance :: Double, angle :: Double }
    
    instance PolarFamily Polar where
      distanceP = distance
      angleP = angle
      toMe p = p
    

    twstwa的定义如下:

    -- .................................................
    -- .................................................
    data TwsTwa = TwsTwa { tws :: Double, twa :: Double }
    
    instance PolarFamily TwsTwa where
      distanceP = tws
      angleP = twa
      toMe Polar{ distance=d, angle=a} = TwsTwa {tws=d, twa=a}
    
    2 回复  |  直到 6 年前
        1
  •  4
  •   HTNW    6 年前

    type

    type TwsTwa = Polar

    TwsTwa Polar

    tws :: TwsTwa -> Double twa :: TwsTwa -> Double

    tws :: TwsTwa -> Double
    tws = distance
    
    twa :: TwsTwa -> Double
    twa = angle
    

    {-# LANGUAGE PatternSynonyms #-}
    
    pattern TwsTwa :: Double -> Double -> Polar
    pattern TwsTwa { tws, twa } = Polar twa tws
    -- can use record syntax
    -- including field accesses, record updates, and record construction
    

    • type MightError a = Either String a
      type TwoList a = ([a], [a])
      
    • type Time = Int
      

      Time Integer

        2
  •  2
  •   Welperooni    6 年前

    import Control.Lens
    
    newtype Polar = Polar { unPolar :: (Double,Double) } deriving Show
    newtype TwsTwa = TwsTwa { unTws :: (Double,Double) } deriving Show
    
    class ToTup a where
      -- You could make them accept any type instead of just Double but idk
      iTup :: Iso' (Double,Double) a
      unPol :: a -> (Double,Double)
    
      rotate :: Double -> a -> a
      rotate ang = under iTup (\(x,y) -> (y,x+ang)) 
    
      sumP :: a -> a -> a
      sumP p1 = under iTup (\(x,y) -> unPol p1 & (\(x1,y1) -> (x1+x,y1+y))) 
    
    instance ToTup Polar where
      iTup = iso Polar unPolar 
      unPol = unPolar
    
    instance ToTup TwsTwa where
      iTup = iso TwsTwa unTws
      unPol = unTws