代码之家  ›  专栏  ›  技术社区  ›  Aron Lee

改善haskell功能

  •  3
  • Aron Lee  · 技术社区  · 6 年前

    我有以下的haskell函数

    fun::Vertex3 GLfloat -> Vertex3 GLfloat ->  Vertex3 GLfloat
    fun (Vertex3 x0 y0 z0) (Vertex3 x1 y1 z1) = do sth here
            where
                p0 = (Vertex3 x0 y0 z0)
                p1 = (Vertex3 x1 y1 z1)
                p = p0 + p1
    

    我想知道有没有办法 不重复 (Vertex3 x0 y0 z0) (Vertex3 x1 y1 z1) 代码内部

    我在找一些东西,比如:

    fun::Vertex3 GLfloat -> Vertex3 GLfloat ->  Vertex3 GLfloat
    fun p0 p1 = do sth here
        where 
            p0 = (Vertex3 x0 y0 z0)
            p1 = (Vertex3 x1 y1 z1)
            p = p0 + p1
    
    1 回复  |  直到 6 年前
        1
  •  6
  •   willeM_ Van Onsem    6 年前

    是的,您可以使用 as-pattern [AGItH'98] :

    fun::Vertex3 GLfloat -> Vertex3 GLfloat -> Vertex3 GLfloat
    fun p0@(Vertex3 x0 y0 z0) p1@(Vertex3 x1 y1 z1) = do sth here
            where p = p0 + p1

    因此,我们在这里引用了这两个论点 p0 以及数据构造函数中的元素( x0 , y0 , z0 )

    这些 作为模式 可以在模式中的不同级别使用。

    推荐文章