Section 6.1

第 6.1 节

Example from the tutorial. Note that newtype supports deriving in the same manner as data. We also need to derive a Eq instance for Natural since the definition of Num has Eq as a superclass.

来自教程的例子。注意 newtype 支持与 data 相同方式的 deriving。我们还

需要为 Natural 派生一个 Eq 实例,因为 Num 的定义以 Eq 作为超类。

The Num instance of Natural allows the use of integer constants such as 1 as Naturals.

Natural 的 Num 实例允许把 1 这样的整数常量当作 Natural 使用。

newtype Natural = MakeNatural Integer deriving (Show, Eq)
toNatural :: Integer -> Natural
toNatural x | x < 0     = error "Can't create negative naturals!"
            | otherwise = MakeNatural x
fromNatural :: Natural -> Integer
fromNatural (MakeNatural i) = i
instance Num Natural where
    fromInteger = toNatural
    x + y = toNatural (fromNatural x + fromNatural y)
    x - y = let r = fromNatural x - fromNatural y in
              if r < 0 then error "Unnatural subtraction"
                        else toNatural r
    x * y = toNatural (fromNatural x * fromNatural y)
e1 :: Natural
e1 = toNatural 1
e2 :: Natural
e2 = 1
e3 :: Natural
e3 = 1 + 1
e4 :: Natural
e4 = (3 - 4) + 3

Section 6.2

第 6.2 节

data Point = Pt {pointx, pointy :: Float} deriving Show
absPoint :: Point -> Float
absPoint p = sqrt (pointx p * pointx p + pointy p * pointy p)
e5 :: Point
e5 = Pt {pointx = 1, pointy = 2}
e6 :: Float
e6 = absPoint e5
e7 :: Float
e7 = pointx e5
e8 :: Point
e8 = e5 {pointx = 4}
data T = C1 {f :: Int, g :: Float}
       | C2 {f :: Int, h :: Bool} deriving Show
e9 :: T
e9 = C1 {f = 1, g = 2}
e10 :: T
e10 = C2 {f = 3, h = False}
e11 :: Int
e11 = f e9
e12 :: Int
e12 = f e10
e13 :: Float
e13 = g e10

Section 6.3

第 6.3 节

Here is a definition of head-strict lists: the head of each list is evaluated when the list is constructed.

这里定义了头严格的(head-strict)列表:每个列表的头部在列表构造时

就会被求值。

data HList a = Cons !a (HList a) | Nil deriving Show
hd (Cons x y) = x
tl (Cons x y) = y

If the "!" is removed then e17 no longer is an error.

如果去掉 "!",那么 e17 就不再是错误了。

e14 :: HList Bool
e14 = True `Cons` (error "e14" `Cons` Nil)
e15, e16 :: Bool
e15 = hd e14
e16 = hd (tl e14)
e17 :: HList Bool
e17 = tl (tl (e14))