我尝试使用下面的代码打印在Haskell链表:变量未在范围上的错误使用功能卫士
data List = Node {value:: Double, next:: List}
| Empty
printList :: List -> String
printList x | x == (Node v n) = show v ++ " " ++ printList n
| otherwise = show '_'
和获取编译错误:
:load scratch.hs
[1 of 1] Compiling Main (scratch.hs, interpreted)
scratch.hs:5:26: error: Variable not in scope: v :: Double
scratch.hs:5:28: error: Variable not in scope: n :: List
scratch.hs:5:38: error: Variable not in scope: v
scratch.hs:5:53: error: Variable not in scope: n :: List
Failed, modules loaded: none.
虽然我能够使用没有警卫的模式匹配来做同样的事情。
printList (Node v n) = show v ++ " " ++ printList n
printList Empty = ""
第一个代码有什么问题?
在'的printList(节点VN)','v'和'N'是参数,但在'x ==(Node vn)'中,它们是两个未定义的变量。 – ForceBru
@ForceBru所以有办法区分哪个数据构造函数x正在使用? –