計算機科学のブログ

実践Haskell HaskellのエラーとEither型 部分関数、安全なバージョン、succ関数、tail関数、last関数、Maybe型

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT7(実践Haskell)、LESSON 38(HaskellのエラーとEither型)、38.5(練習問題)Q38-2の解答を求めてみる。

コード

lesson/app/Main.hs

module Main (main) where

import Lib
  ( myLast,
    mySucc,
    myTail,
  )

main :: IO ()
main = do
  print $ mySucc (1 :: Int)
  print $ mySucc (maxBound :: Int)
  print $ myTail ([1 .. 5] :: [Int])
  print $ myTail ([] :: [Int])
  print $ myLast ([1 .. 5] :: [Int])
  print $ myLast ([] :: [Int])
  print $ myLast ([1 ..] :: [Int])

lesson/src/Lib.hs

module Lib
  ( mySucc,
    myTail,
    myLast,
  )
where

mySucc :: (Eq a, Bounded a, Enum a) => a -> Maybe a
mySucc x
  | x == maxBound = Nothing
  | otherwise = Just $ succ x

myTail :: [a] -> [a]
myTail [] = []
myTail (_ : xs) = xs

myLast :: [b] -> Either String b
myLast = myLast' (10000 :: Int)

myLast' :: (Eq t, Num t) => t -> [b] -> Either String b
myLast' _ [] = Left "empty list"
myLast' _ [x] = Right x
myLast' 0 _ = Left "invalid length list"
myLast' n (_ : xs) = myLast' (n - 1) xs

入出力結果(Terminal, Zsh)

% stack exec lesson-exe 
Just 2
Nothing
[2,3,4,5]
[]
Right 5
Left "empty list"
Left "invalid length list"
%