为什么 foldr const 0 "tacos" 不在 Haskell 中编译?

Car*_*erg 0 haskell fold

所以foldr const 0 "tacos"应该解开成为类似的东西

0 const ('t' const ('a' const ('c' const ('o' const 's')))). 
Run Code Online (Sandbox Code Playgroud)

我认为它只会停止,0 const ('t'因为 Haskell 是惰性求值的,而 const 只接受第一个参数。那么,理论上,该函数不会适用于 1 吗?

但这不起作用。替换0""也不起作用。有谁知道为什么?

谢谢!

Tho*_*son 5

为什么 foldr const 0 “tacos” 不能在 Haskell 中编译?

因为:

> foldr const 0 "tacos"

<interactive>:1:13: error:
    • No instance for (Num Char) arising from the literal ‘0’
    • In the second argument of ‘foldr’, namely ‘0’
      In the expression: foldr const 0 "tacos"
      In an equation for ‘it’: it = foldr const 0 "tacos"
Run Code Online (Sandbox Code Playgroud)

因此,如果我们调查我们会看到(请忽略@[],它是一个使我们的对话更简单的类型应用程序)

> :t foldr @[] const 0
foldr @[] const 0 :: Num b => [b] -> b
Run Code Online (Sandbox Code Playgroud)

因此,您需要提供作为Num实例的项目列表。但是你提供了:

> :t "tacos"
"tacos" :: [Char]
Run Code Online (Sandbox Code Playgroud)

炸玉米饼有时是包裹在肉和米饭上的玉米饼。但有时,例如现在,它们是字符列表。字符不是Num. 这就是错误试图告诉你的。