Created
March 4, 2015 11:58
-
-
Save shigemk2/4b56992e59aa23e71bd8 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 Point = Point Float Float deriving (Show) | |
data Shape = Circle Point Float | Rectangle Point Point deriving (Show) | |
area :: Shape -> Float | |
area (Circle _ r) = pi * r ^ 2 | |
area (Rectangle (Point x1 y1) (Point x2 y2)) | |
= (abs $ x2 - x1) * (abs $ y2 - y1) | |
nudge :: Shape -> Float -> Float -> Shape | |
nudge (Circle (Point x y) r) a b = Circle (Point (x+a) (y+b)) r | |
nudge (Rectangle (Point x1 y1) (Point x2 y2)) a b | |
= Rectangle (Point (x1+a) (y1+b)) (Point (x2+a) (y2+b)) | |
-- 指定したサイズの図形を原点に作る補助関数を作って、それから移動させる | |
baseCircle :: Float -> Shape | |
baseCircle r = Circle (Point 0 0) r | |
-- 幅と高さを取って、左下の頂点が原点にある長方形を作る関数 | |
baseRect :: Float -> Float -> Shape | |
baseRect width height = Rectangle (Point 0 0) (Point width height) | |
main = do | |
print $ area (Rectangle (Point 0 0) (Point 100 100)) | |
print $ area (Circle (Point 0 0) 24) | |
print $ nudge (Circle (Point 34 34) 10) 5 10 | |
print $ nudge (baseRect 40 100) 60 23 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment