3

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

另外我认为指南中有一个错误:“then group using clauses”的示例是不可能的,因为“(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])
4

1 回答 1

0

你可以重写

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

更正确地为

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

与此同时,该groupBy错误似乎已得到修复,文章的示例有效。

这个扩展的另一个有用的来源是在这个博客文章中,也可以看到原文章综合理解

于 2017-12-27T08:37:15.980 回答