Last active
August 29, 2015 13:58
-
-
Save munro/9942933 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data Box = Box { box_x1 :: Integer, box_y1 :: Integer, box_x2 :: Integer, box_y2 :: Integer } deriving Show | |
subdivideNW Box { box_x1 = x1', box_y1 = y1', box_x2 = x2', box_y2 = y2'} = Box { | |
box_x1 = x1', | |
box_y1 = y1', | |
box_x2 = x1' + half_width, | |
box_y2 = y1' + half_height | |
} | |
where | |
half_width = (x2' - x1') `div` 2 | |
half_height = (y2' - y1') `div` 2 | |
-- could I do something like this instead? where I redundantly assign variables? | |
half_width Box { box_x1 = x1', box_y1 = _, box_x2 = x2', box_y2 = _ } = (x2' - x1') `div` 2 | |
half_height Box { box_x1 = _, box_y1 = y1', box_x2 = _, box_y2 = y2' } = (y2' - y1') `div` 2 | |
subdivideNW Box box' { box_x1 = x1', box_y1 = y1', box_x2 = x2', box_y2 = y2'} = Box { | |
box_x1 = x1', | |
box_y1 = y1', | |
box_x2 = x1' + (half_width box'), | |
box_y2 = y1' + (half_height box') | |
} | |
-- hell yea I can! | |
-- 13:57 < dhrosa> felixn: maybe you want myvar@Box {...} | |
-- 13:57 < dhrosa> felixn: that will let you bind the inner stuff to variables, | |
-- and also the entire structure | |
-- 13:58 < dhrosa> felixn: you can also use that syntax on any constructor, like f | |
-- x@(y:z) | |
half_width Box { box_x1 = x1', box_y1 = _, box_x2 = x2', box_y2 = _ } = (x2' - x1') `div` 2 | |
half_height Box { box_x1 = _, box_y1 = y1', box_x2 = _, box_y2 = y2' } = (y2' - y1') `div` 2 | |
subdivideNW box@Box { box_x1 = x1', box_y1 = y1', box_x2 = x2', box_y2 = y2'} = Box { | |
box_x1 = x1', | |
box_y1 = y1', | |
box_x2 = x1' + (half_width box), | |
box_y2 = y1' + (half_height box) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment