Haskell - 実践Haskell - HaskellのエラーとEither型 - Left, Right, エラーメッセージ
入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)の UNIT 7(実践Haskell)、LESSON 38(HaskellのエラーとEither型)、38.3(Either型)、 クイックチェック 38-2の解答を求めてみる。
コード
sample3.hs
intExample :: [Int]
intExample = [1, 2, 3]
eitherHead :: [a] -> Either String a
eitherHead [] = Left "There is no head because the list is empty"
eitherHead (x:xs) = Right x
main :: IO ()
main = do
print $ (+) <$> eitherHead intExample <*> eitherHead (tail intExample)
入出力結果(Terminal, Zsh)
% runghc sample3.hs
Right 3
%