Skip to content

Instantly share code, notes, and snippets.

@motokiee
Created October 28, 2015 05:08
Show Gist options
  • Save motokiee/40454e09759e6a796895 to your computer and use it in GitHub Desktop.
Save motokiee/40454e09759e6a796895 to your computer and use it in GitHub Desktop.
あまり進捗はなかった #CodePiece
module Shapes
( Point, Shape, area, nudge, baseCircle, baseRect ) where
-- 値コンストラクタが1つしかない場合、データ型と値コンストラクタは同じ名前にする(慣例)
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 w h = Rectangle (Point 0 0) (Point w h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment