As part of Graham's Scan, I am writing a function that determines whether the line is turning left or right at a certain point. Considering we have a function determinant point -> point -> point -> int
where determinant p0 p1 p2
returns det(p0p1, p0p2), here is the code :
(* Determines how the line turns at s, when going from t to p*)
let turn t p s =
let det = det s p t in
if det < 0 then
'r' (* Turning right *)
else if det > 0 then
'l'(* Turning left *)
else
's' (* Going straight ahead *)
;;
Is there any way to do this with pattern matching instead ? Here is what I have tried, which obviously doesn't work :
let turn t p s =
match det s p t with
| < 0 -> 'r'
| 0 -> 's'
| > 0 -> 'l'
;;
( Note that the variable / function names are determined by the class ; while I do find single letter names slightly confusing, I may not change them... )
No.
Well...
Conditional guards on patterns could do what you're suggesting.
Would become:
Which of these expresses what's going on more cleanly is a matter of opinion.
What about...?
The argument becomes more interesting with more complex data to match on. A very contrived example with far better actual implementations follows.
Using conditional guards:
Still a matter of opinion, but conditional guards can help to "flatten" nested conditionals in pattern-matching.