1

我想知道是否有人可以帮助我确定 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
4

1 回答 1

1

正如 Benjamin Hodgson 指出的那样,您没有匹配rules字符串非空((_ : _)而不是[])但Stack为空的情况。这种情况会立即出现,因为初始堆栈trace传递给traceStateis 事实上[]

要修复警告/异常,您需要为traceStateas重写第二个等式traceState (x : xs) t y或编写第三个等式tracestate (x : xs) t []. 使用前一种解决方案,您可以case y ofy. 无论哪种方式,您都需要确定如何处理列表为空的情况。

这个问题实际上会出现在类似的调用traceState "]" t []中,即如果第一个参数以trace“]”或“F”开头。看起来那将是一个无效的输入。如果是这种情况,您可能想要重写tracetraceState返回 a Maybe [ColouredLine],或用于Either提供错误消息。

于 2014-10-30T20:41:48.180 回答