計算機科学のブログ

Haskell - コンテキストでの型の操作 - Functor型クラス - instance, fmap関数

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)の UNIT5(コンテキストでの型の操作)、LESSON 27(Functor型クラス)、27.5(練習問題)Q27-1の解答を求めてみる。

コード

sample.hs

main :: IO ()
main = do
    print $ fmap (+ 1) b
    print $ (+ 1) <$> b 

data Box a = Box a deriving Show

instance Functor Box where
    fmap func (Box x) = Box $ func x

b :: Box Int
b = Box 10

入出力結果(Terminal, Zsh)

% runghc sample.hs
Box 11
Box 11
%