3

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])
Ingdas
  • 1,446
  • 1
  • 11
  • 21

1 Answers1

0

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.

robx
  • 2,221
  • 1
  • 14
  • 31