I'm working on some CS homework and this is my first experience with Haskell, so I have a question.
I've declared a data type
data Date = Date Int Int Int deriving (Show)
and I have this function testdate:
testdate :: Date -> Maybe Date
testdate (Date m d y) = if 1 <= m && m <= 12 && 1 <= d && d <= 31 && y >= 0
then
True
else
False
I want to make a new function that checks to see if testdate is true or false, then returns to you the date. something like:
betterdate :: Date -> Maybe Date
betterdate (Date m d y) if (testdate = True) //I know this part doesn't work
then Just (Date m d y)
else Nothing
How would I do this?
You want to pass your date to the testdate function. This should work:
betterdate d = if testdate d
then Just d
else Nothing
If you really want to unpack the Date in the parameter list then you can do this to avoid duplicating (Date m d y) everwhere:
betterdate date@(Date m d y) = if testdate date
then Just date
else Nothing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With