考虑以下不良代码:
fun x =
if (null x) then 0
else (take 50 x) : (fun (drop 50 x))
Run Code Online (Sandbox Code Playgroud)
我注意到,我可以毫无问题地将它加载到ghci中,这就是问题所在.
当我尝试评估此功能时,程序只检索到一个错误.
关于if ... then ... else表达式的默认推理规则,当两个分支显式检索不同的类型时,为什么这个代码可以加载(即编译)?我的意思是,为什么编译器无法弄清楚这个代码是不正确的?
注意:当然,如果我为此函数添加正确的类型注释,正如预期的那样它将被拒绝,但在我的理解中,它也应该在没有类型注释的情况下被拒绝.
为了进入上下文,我正在将列表理解(从那里)转换为"模式过滤"到其monadic对应物(do和bind),然后我遇到异常.
我从这些定义开始(bf为breadthFirst持有),
上下文,数据和帮助器
data Tree a = Leaf | Node a (Tree a) (Tree a) deriving Show
let test = Node 1 (Node 2 (Node 4 Leaf Leaf) Leaf) (Node 3 Leaf (Node 5 Leaf Leaf))
let nextChild = concatMap (\x -> case x of; Leaf -> []; Node n l r -> [l,r])
Run Code Online (Sandbox Code Playgroud)
功能测试
let bfLc xs | null xs = [] | otherwise = [ n | Node n _ _ <- xs] ++ (bfLc $ …Run Code Online (Sandbox Code Playgroud)