Skip to content

Instantly share code, notes, and snippets.

@shigemk2 shigemk2/shape.hs
Created Mar 4, 2015

Embed
What would you like to do?
data Shape = Circle Float Float Float |
Rectangle Float Float Float Float
-- 分解しながら代入する
-- パターンマッチで分解
area :: Shape -> Float
area (Circle _ _ r) = pi * r ^ 2
area (Rectangle x1 y1 x2 y2) = (abs $ x2 - x1) * (abs $ y2 - y1)
main = do
print $ area $ Circle 10 20 10 -- 314.15927
print $ area $ Rectangle 0 0 100 100 -- 10000.0
print $ area $ c 100 -- 部分適用
where
c = Circle 1 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.