【问题标题】:Haskell TransformListComp extensionHaskell TransformListComp 扩展
【发布时间】:2013-03-04 08:50:59
【问题描述】:

我阅读了this guide 关于haskell 语言扩展的内容,并且对TransformListComp 的解释感到有些困惑。我尝试在不加糖的情况下重写所有 TransformListComp 表达式,但我不确定我是否正确。

我还认为指南中有一个错误:“然后使用子句分组”的示例是不可能的,因为“(groupBy(==))”不是正确的类型(“Eq a”不能是使用)

[foo |  x1 <- xs1,
        x2 <- xs2,
        ...
        xi <- xsi,
        then f,
        xj <- xsj,
        ...
        xn <- xni
    ]

==
[foo |  f x1 <- xs1,
        f x2 <- xs2,
        ...
        f xi <- xsi,
        xj <- xsj,
        ...
        xn <- xni
    ]

-------------------


[foo |  x1 <- xs1,
        x2 <- xs2,
        ...
        xi <- xsi,
        then f by exp,
        xj <- xsj,
        ...
        xn <- xni
    ]

==

f (\(x1,x2,...,xi) -> exp) [(x1,x2,...,xi) | 
        x1 <- xs1,
        x2 <- xs2,
        ...
        xi <- xsi]

>>=(\(x1,x2,...,xi) -> 
    [foo |  
        xj <- xsj,
        ...
        xn <- xni
    ])


-------------------

[foo |  x1 <- xs1,
        x2 <- xs2,
        ...
        xi <- xsi,
        then group using f,
        xj <- xsj,
        ...
        xn <- xni
    ]

==

map unzipI (f [(x1,x2,...,xi) | 
        x1 <- xs1,
        x2 <- xs2,
        ...
        xi <- xsi])

>>=(\(xs1,xs2,...,xsi) -> 
    [foo |
        x1 <- xs1,
        x2 <- xs2,
        ...
        xi <- xsi,
        xj <- xsj,
        ...
        xn <- xni
    ])

unzipI :: [(t1,t2,...tn)] -> ([t1],[t2]..,[tn])

-------------------

[foo |  x1 <- xs1,
        x2 <- xs2,
        ...
        xi <- xsi,
        then group by exp using f,
        xj <- xsj,
        ...
        xn <- xni
    ]

==

 map unzipI (f (\(x1,x2,...,xi) -> exp) [(x1,x2,...,xi) | 
        x1 <- xs1,
        x2 <- xs2,
        ...
        xi <- xsi])

>>=(\(xs1,xs2,...,xsi) -> 
    [foo |
        x1 <- xs1,
        x2 <- xs2,
        ...
        xi <- xsi,
        xj <- xsj,
        ...
        xn <- xni
    ])

unzipI :: [(t1,t2,...tn)] -> ([t1],[t2]..,[tn])

【问题讨论】:

    标签: haskell language-extension


    【解决方案1】:

    你可以重写

    [ foo | x1 <- xs1
          , x2 <- xs2
          , then f
          , x3 <- xs3 ]
    

    更准确地说

    [ foo | (x1, x2) <- f [ (x1, x2) | x1 <- xs1
                                     , x2 <- xs2 ]
          , x3 <- xs3 ]
    

    groupBy 错误似乎在此期间已修复,文章的示例有效。

    关于这个扩展的另一个有用的来源是这个blog post,另见原始文章comprehensive comprehensions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 2013-12-31
      • 2023-04-03
      • 1970-01-01
      • 2011-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多