計算機科学のブログ

実践Haskell HaskellのエラーとEither型 head関数、部分関数、エラー maximum関数、succ関数

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT7(実践Haskell)、LESSON 38(HaskellのエラーとEither型)、38.1(head関数、部分関数、エラー)、head関数と部分関数、クイックチェック 38-2の解答を求めてみる。

入出力結果(Terminal, Zsh)

% ghci
GHCi, version 8.10.7: https://www.haskell.org/ghc/  :? for help
macro 'doc' overwrites builtin command.  Use ':def!' to overwrite.
(0.00 secs, 0 bytes)
(0.00 secs, 0 bytes)
Loaded GHCi configuration from /Users/…/.ghc/ghci.conf
Prelude
λ> maximum []
*** Exception: Prelude.maximum: empty list
Prelude
λ> succ (maxBound :: Int)
*** Exception: Prelude.Enum.succ{Int}: tried to take `succ' of maxBound
Prelude
λ> maximum [5, 1, 4, 2, 3]
5
it :: (Ord a, Num a) => a
(0.01 secs, 64,248 bytes)
Prelude
λ> succ $ (maxBound :: Int) - 1
9223372036854775807
it :: Int
(0.01 secs, 77,048 bytes)
Prelude
λ> maxBound :: Int
9223372036854775807
it :: Int
(0.00 secs, 76,904 bytes)
Prelude
λ> :q
Leaving GHCi.
%