为什么这个Haskell代码失败了?

Ben*_*Ben 1 haskell

我有以下代码(与Project Euler的问题34相关):

fac :: Int -> Int
fac = foldr1 (*) . enumFromTo 1

fe :: Int -> Bool
fe = (==) =<< (sum . map fac . digits 10)

fes :: [Int]
fes = filter fe [3..]
Run Code Online (Sandbox Code Playgroud)

fac工作正常,fe工作正常,但fes给出:

*** Exception: Prelude.foldr1: empty list
Run Code Online (Sandbox Code Playgroud)

有趣的是,如果我用145开始列表(fe在这里返回True),它适用于该数字,然后引发异常.

为什么这段代码失败了?这似乎是一个完全正常的应用程序,filter具有明显有效的功能和单独使用该功能的常规数字列表.

Ørj*_*sen 6

Your fac fails with digit 0, for which its list is empty. You can try

fac = foldr (*) 1 . enumFromTo 1
Run Code Online (Sandbox Code Playgroud)

By the way foldl' (from Data.List) is probably better than foldr here, also there's a predefined product function.