Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Created March 4, 2015 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shigemk2/c65d29c1a0f5c1033c9a to your computer and use it in GitHub Desktop.
Save shigemk2/c65d29c1a0f5c1033c9a to your computer and use it in GitHub Desktop.
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