我不理解以下代码的新类型定义;
newtype Prod a = Prod { getProd :: a }
instance Num a => Monoid (Prod a) where
mempty = Prod 1
Prod x `mappend` Prod y = Prod (x * y)
Run Code Online (Sandbox Code Playgroud)
然后我运行以下查询:
Ghci > getProd $ Prod 2 `mappend` Prod 5
-> 10
Run Code Online (Sandbox Code Playgroud)
我如何获得此结果以及getProd如何参与此结果?
我懂了:
type ID = Int
data Bank = Bank [(ID, Account)] deriving Show
data Account = Account
{ balance :: Int
, owner :: Client
} deriving Show
data Client = Client
{ name :: String
, surname :: String
, address :: String
} deriving Show
Run Code Online (Sandbox Code Playgroud)
我的任务是,编写一个函数credit :: Int -> ID -> Bank -> Bank
,将指定金额的Money添加到指定金额的帐户.我不知道我怎么能这样做,因为这些是数据类型..