误解List monad

Mok*_*sha 1 monads haskell

我很困惑为什么我的程序得到以下输出:

-- test.hs

f :: Int -> [[Int]]
f 0 = []
f x = do
  y <- [0, 1]
  g <- f (x - 1)
  return (y : g)

main :: IO ()
main = print $ f 2
Run Code Online (Sandbox Code Playgroud)

我希望这个程序的输出是

[[0, 0], [0, 1], [1, 0], [1, 1]]
Run Code Online (Sandbox Code Playgroud)

但是,我得到的只是

$ ghc -o test test.hs && ./test
[]
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

小智 10

尝试

f 0 = [[]]

看起来fx生成长度为x的按位组合,因此f 0必须包含长度为0的序列,即空列表.