2013-03-15 51 views
3

我讀了關於haskell語言擴展的this guide,並對TransformListComp解釋有些困惑。我試圖重寫所有沒有糖的TransformListComp表達式,但我不確定我是否正確。Haskell 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]) 

回答

0

你可以更正確地改寫

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

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

的在此期間3210錯誤似乎已被修復,文章的例子起作用。

此擴展的另一個有用的來源是在這blog post,也見原始文章comprehensive comprehensions