Skip to content

Instantly share code, notes, and snippets.

@mizukmb
Last active Jul 15, 2017
Embed
What would you like to do?
module Shapes (Point, Shape, area, nudge, baseCircle, baseRect) where
-- data Shape = Circle Float Float Float |
-- Rectangle Float Float Float Float
-- data Shape = Circle Float Float Float |
-- Rectangle Float Float Float Float
-- deriving (Show)
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 x1 y1 x2 y2) = (abs $ x2 - x1) * (abs $ y2 - y1)
area :: Shape -> Float
area (Circle _ r) = pi * r ^ 2
area (Rectangle (Point x1 y1) (Point x2 y2)) = (abs $ x2 - x1) * (abs $ y2 - y1)
baseCircle :: Float -> Shape
baseCircle r = Circle (Point 0 0) r
baseRect :: Float -> Float -> Shape
baseRect w h = Rectangle (Point 0 0) (Point w h)
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))
@mizukmb
Copy link
Author

mizukmb commented Jul 15, 2017

すごいHaskellたのしく学ぼう! 『第7章 型や型クラスを自分で作ろう』より

@mizukmb
Copy link
Author

mizukmb commented Jul 15, 2017

import Shapes
-- main = do
--   print $ Circle (Point 10 20) 30
-- #=> Data constructor not in scope: Circle :: t0 -> Integer -> a0
-- #=> Data constructor not in scope: Point :: Integer -> Integer -> t0
main = do
  print $ nudge (baseCircle 30) 10 20

コメントアウトした部分はエラーになる。 Circle Point Float という値コンストラクタを隠蔽したため

@mizukmb
Copy link
Author

mizukmb commented Jul 15, 2017

module Shapes (Point(..), Shape(..), ...) と書けば値コンストラクタもエクスポートされる

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment