tfwh-0.3.7.0

Safe HaskellSafe
LanguageHaskell2010

TFwH.Chap03.ExG

Description

第3章 練習問題 G

  • Nat を型クラスOrd のインスタンスとして定義せよ.そのうえで,以下のdivMod の定義を構成せよ. divMod :: Nat -> Nat -> (Nat, Nat)
instance Ord Nat where
  Zero <= Zero   = True
  Zero <= Succ n = True
  Succ m <= Zero = False
  Succ m <= Succ n = m <= n

divMod :: Nat -> Nat -> (Nat, Nat)
divMod x y = if x < y then (Zero, x)
             else (Succ q, r)
  where
    (q, r) = divMod (x - y) y

Synopsis

Documentation

data Nat Source #

自然数

Constructors

Zero 
Succ Nat 

Instances

Eq Nat Source # 

Methods

(==) :: Nat -> Nat -> Bool #

(/=) :: Nat -> Nat -> Bool #

Num Nat Source #

数値 Num クラスのインスタンス

Methods

(+) :: Nat -> Nat -> Nat #

(-) :: Nat -> Nat -> Nat #

(*) :: Nat -> Nat -> Nat #

negate :: Nat -> Nat #

abs :: Nat -> Nat #

signum :: Nat -> Nat #

fromInteger :: Integer -> Nat #

Ord Nat Source #

順序 Ord クラスのインスタンス

Methods

compare :: Nat -> Nat -> Ordering #

(<) :: Nat -> Nat -> Bool #

(<=) :: Nat -> Nat -> Bool #

(>) :: Nat -> Nat -> Bool #

(>=) :: Nat -> Nat -> Bool #

max :: Nat -> Nat -> Nat #

min :: Nat -> Nat -> Nat #

Show Nat Source # 

Methods

showsPrec :: Int -> Nat -> ShowS #

show :: Nat -> String #

showList :: [Nat] -> ShowS #

divMod :: Nat -> Nat -> (Nat, Nat) Source #

Nat 上の剰余算