Advertisement
NLinker

Pattern matching for directions

Jul 4th, 2017
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. data Diff = Diff { x :: Float, y :: Float, z :: Float }
  2.  
  3. data Direction =
  4.     DIR_UP_NORTH_WEST
  5.   | DIR_UP_SOUTH_WEST
  6.   | DIR_UP_WEST
  7.   | DIR_UP_NORTH_EAST
  8.   | DIR_UP_SOUTH_EAST
  9.   | DIR_UP_EAST
  10.   | DIR_UP_NORTH
  11.   | DIR_UP_SOUTH
  12.   | DIR_UP
  13.   | DIR_DOWN_NORTH_WEST
  14.   | DIR_DOWN_SOUTH_WEST
  15.   | DIR_DOWN_WEST
  16.   | DIR_DOWN_NORTH_EAST
  17.   | DIR_DOWN_SOUTH_EAST
  18.   | DIR_DOWN_EAST
  19.   | DIR_DOWN_NORTH
  20.   | DIR_DOWN_SOUTH
  21.   | DIR_DOWN
  22.   | DIR_NORTH_WEST
  23.   | DIR_SOUTH_WEST
  24.   | DIR_WEST
  25.   | DIR_NORTH_EAST
  26.   | DIR_SOUTH_EAST
  27.   | DIR_EAST
  28.   | DIR_NORTH
  29.   | DIR_SOUTH
  30.   | DIR_NONE
  31.  
  32. test :: Diff -> Direction
  33. test diff =
  34.   let zero = 0.0 in
  35.   let c1 = compare zero (x diff) in
  36.   let c2 = compare zero (y diff) in
  37.   let c3 = compare zero (z diff) in
  38.   decide (c1, c2, c3)
  39.   where
  40.     decide :: (Ordering, Ordering, Ordering) -> Direction
  41.     decide (LT, LT, LT) = DIR_UP_NORTH_WEST
  42.     decide (LT, LT, EQ) = DIR_UP_SOUTH_WEST
  43.     decide (LT, LT, GT) = DIR_UP_WEST
  44.     decide (LT, EQ, LT) = DIR_UP_NORTH_EAST
  45.     decide (LT, EQ, EQ) = DIR_UP_SOUTH_EAST
  46.     decide (LT, EQ, GT) = DIR_UP_EAST
  47.     decide (LT, GT, LT) = DIR_UP_NORTH
  48.     decide (LT, GT, EQ) = DIR_UP_SOUTH
  49.     decide (LT, GT, GT) = DIR_UP
  50.     decide (EQ, LT, LT) = DIR_DOWN_NORTH_WEST
  51.     decide (EQ, LT, EQ) = DIR_DOWN_SOUTH_WEST
  52.     decide (EQ, LT, GT) = DIR_DOWN_WEST
  53.     decide (EQ, EQ, LT) = DIR_DOWN_NORTH_EAST
  54.     decide (EQ, EQ, EQ) = DIR_DOWN_SOUTH_EAST
  55.     decide (EQ, EQ, GT) = DIR_DOWN_EAST
  56.     decide (EQ, GT, LT) = DIR_DOWN_NORTH
  57.     decide (EQ, GT, EQ) = DIR_DOWN_SOUTH
  58.     decide (EQ, GT, GT) = DIR_DOWN
  59.     decide (GT, LT, LT) = DIR_NORTH_WEST
  60.     decide (GT, LT, EQ) = DIR_SOUTH_WEST
  61.     decide (GT, LT, GT) = DIR_WEST
  62.     decide (GT, EQ, LT) = DIR_NORTH_EAST
  63.     decide (GT, EQ, EQ) = DIR_SOUTH_EAST
  64.     decide (GT, EQ, GT) = DIR_EAST
  65.     decide (GT, GT, LT) = DIR_NORTH
  66.     decide (GT, GT, EQ) = DIR_SOUTH
  67.     decide (GT, GT, GT) = DIR_NONE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement