Input checks in Haskell data constructors
问题 How can I add input checks to Haskell data constructors? Let's say I have import Data.Time.Calendar data SchedulePeriod = SchedulePeriod { startDate :: Day , endDate :: Day , accrualStart :: Day , accrualEnd :: Day , resetDate :: Day , paymentDate :: Day , fraction :: Double } deriving (Show) and I want to impose a constraint startDate < endDate . Is there a way to do it without creating an abstract data type? 回答1: The standard way is to use a smart constructor that checks the precondition