Reputation: 91
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
Upvotes: 1
Views: 183
Reputation: 5992
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.
Upvotes: 1