不能构造无限类型:e〜[e]

Sha*_*iar 0 haskell

为什么这段代码:

a=array ((0,0),(5,5)) [((i,j),x) | i <- [0..5], j <- [0..5], x <- a!(i,j)]
Run Code Online (Sandbox Code Playgroud)

导致错误cannot construct the infinite type: e ~ [e],但如果像这样重写:

a=array ((0,0),(5,5)) [((i,j),a!(i,j)) | i <- [0..5], j <- [0..5]]
Run Code Online (Sandbox Code Playgroud)

它工作正常吗?

luq*_*qui 5

在列表理解中,右侧<-是从中获取元素的列表.但你可以使用let表达式:

[ ... | ..., let x = a ! (i,j) ]
Run Code Online (Sandbox Code Playgroud)