小编kei*_*thb的帖子

类型和模块如何交互?

为了掌握更好的类型类(从几乎从形式开始)我开始使用面积计算来建模二维形状,如下所示:

module TwoDShapes where

class TwoDShape s where
    area :: s -> Float

data Circle = Circle Float deriving Show
aCircle radius | radius < 0 = error "circle radius must be non-negative"
               | otherwise  = Circle radius
instance TwoDShape Circle where
    area (Circle radius) = pi * radius * radius

data Ellipse = Ellipse Float Float deriving Show
anEllipse axis_a axis_b | axis_a < 0 || axis_b < 0 = error "ellipse axis length must be non-negative"
                        | otherwise …
Run Code Online (Sandbox Code Playgroud)

haskell visibility module typeclass

4
推荐指数
1
解决办法
723
查看次数

标签 统计

haskell ×1

module ×1

typeclass ×1

visibility ×1