简体   繁体   中英

Haskell TransformListComp extension

I read this guide about haskell language extensions and was somewhat confused by the TransformListComp explanation. I tried to rewrite all the TransformListComp expression without the sugar but I'm not sure if I'm correct.

Also I think there is a mistake in the guide: The example for "then group using clauses" is impossible as "(groupBy (==))" is not of the right type ("Eq a" can't be used)

[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])

You could rewrite

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

more correctly as

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

The groupBy mistake seems to have been fixed in the meantime, the article's example works.

Another useful source on this extension is in this blog post , also see the original article comprehensive comprehensions .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM