module Positive (toPositive, Positive(unPositive)) where
newtype Positive = Positive { unPositive :: Int }
toPositive :: Int -> Maybe Positive
toPositive n = if (n < 0) then Nothing else Just (Positive n)
The above module doesn't export the constructor, so the only way to build a value of type Positive
is to supply toPositive
with a positive integer, which you can then unwrap using unPositive
to access the actual value.
You can then write a function that only accepts positive integers using:
positiveInputsOnly :: Positive -> ...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…