The real feeling of haskell list generators

As I understand it, the code

l = [(a,b)|a<-[1,2],b<-[3,4]]

equivalently

l = do
  a <- [1,2]
  b <- [3,4]
  return (a,b)

or

[1,2] >>= (\a -> [3,4] >>= (\b -> return (a,b)))

The type of such an expression is [(t, t1)], where t and t1 are in Num.

If I write something like

getLine >>= (\a -> getLine >>= (\b -> return (a,b)))

the interpreter reads two rows and returns a tuple containing them.

But can I use getLine or something similar in list generators?

Expression

[x|x<-getLine]

returns the error "Could not match expected type [t0]' with actual typeIO String ''

But of course, this works in do-notation or use (→ =).

What is the point of list generators and what is the actual difference between them and do-notation?

Is there a type restriction when using the gens list?

+4
source share
2 answers

, , . , [x|x<-getLine] . , , , , , () , .

, [] , GHC, -XMonadComprehensions, , :

Prelude> :set -XMonadComprehensions 
Prelude> [x|x<-getLine]
sdf
"sdf"
+9

Source: https://habr.com/ru/post/1628284/


All Articles