ghci> [1..50] >>= (\x -> guard ('7' `elem` show x) >> return x)
[7,17,27,37,47]
Run Code Online (Sandbox Code Playgroud)
我将示例更改为:
ghci> [1..50] >>= (\x -> if x > 25 then [] else [()] >> return x)
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好。但现在我想要其他 25 个数字:
ghci> [1..50] >>= (\x -> if x > 25 then [()] else [] >> return x)
<interactive>:204:2: error:
• No instance for (Num ()) arising from the literal ‘1’
• In the expression: 1
In the first argument of ‘(>>=)’, …Run Code Online (Sandbox Code Playgroud) haskell ×1