1

I was wondering if anyone could help me identify the part of the haskell code that would be non-exhaustive? I can't see how the base case isn't met at the end of the list.

thanks a lot

Jonny

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
FoxGlove
  • 81
  • 4
  • 3
    You haven't matched `traceState [_] _ []`. If you compile with warnings on, GHC should give an example of an unmatched pattern. – Benjamin Hodgson Oct 30 '14 at 18:40
  • 1
    Pass `-Wall` to ghc or ghci to enable warnings. – chi Oct 30 '14 at 19:34
  • Thanks so much for the help guys! How can I fix this? I've turned on warnings now (thanks Chi!) and tried to fix the un-matched patterns (thanks Benjamin!). I'm still struggling with what to put there to make it correct! – FoxGlove Oct 30 '14 at 20:23

1 Answers1

1

As Benjamin Hodgson noted, you haven't matched the case where the rules string is non-empty ((_ : _) as opposed to []) but the Stack is empty. That case will arise immediately because the initial stack trace passes to traceState is in fact [].

To fix the warning/exception, you'll need to either rewrite the second equation for traceState as traceState (x : xs) t y or to write a third equation, tracestate (x : xs) t []. With the former solution, you can then use case y of to pattern-match on y. Either way, you need to determine how to handle the situation where the list is empty.

The problem will actually arise with a call like traceState "]" t [], i.e. if the first parameter to trace begins with a "]" or "F". It looks like that'd be an invalid input. If that's the case, you probably want to rewrite trace and traceState to return a Maybe [ColouredLine], or use Either to give an error message.

Christian Conkle
  • 5,932
  • 1
  • 28
  • 52