我想知道是否有人可以帮我识别出非详尽的haskell代码部分?我无法看到列表末尾的基本情况是如何得到满足的。
非常感谢
强尼
type Rule
= (Char, String)
type Rules
= [Rule]
type System
= (Float, String, Rules)
cross, triangle, arrowHead, peanoGosper,
dragon, snowflake, tree, bush :: System
type Vertex
= (Float, Float)
type TurtleState
= (Vertex, Float)
type Stack
= [TurtleState]
type ColouredLine
= (Vertex, Vertex, Colour)
trace :: String -> Float -> Colour -> [ColouredLine]
trace rules angle colour = traceState rules ((0,0),90) []
where
traceState :: String -> TurtleState -> Stack -> [ColouredLine]
traceState [] _ _ = []
traceState (x:xs) t (y:ys)
|x == '[' = traceState xs t (t : (y:ys))
|x == ']' = traceState xs t ys
|x == 'F' = biggieSmalls : traceState xs t (nextY:ys)
|otherwise = traceState xs angledY (angledY:ys)
where
biggieSmalls = (fst(t),fst(nextY),colour)
nextY = move 'F' t angle
angledY = move x t angle
答案 0 :(得分:1)
正如Benjamin Hodgson所指出的,你还没有匹配rules
字符串非空((_ : _)
而不是[]
)而Stack
的情况。 } 是空的。这种情况会立即出现,因为初始堆栈trace
传递给traceState
实际上是[]
。
要修正警告/例外,您需要将traceState
的第二个等式重写为traceState (x : xs) t y
,或者编写第三个等式tracestate (x : xs) t []
。使用前一种解决方案,您可以使用case y of
在y
上进行模式匹配。无论哪种方式,您都需要确定如何处理列表为空的情况。
问题实际上会出现在traceState "]" t []
之类的调用中,即trace
的第一个参数是以"]"或" F"。看起来这是一个无效的输入。如果是这种情况,您可能希望重写trace
和traceState
以返回Maybe [ColouredLine]
,或使用Either
来提供错误消息。