Coeurjoli Martinez
Coeurjoli Martinez

Reputation: 87

nested if statement not working in SML

I am new to SML and I have write a program that takes two years and compares them, then takes two months and compares them, and finally two dates.

The problem im having is that if the year is older than the first it should stop and false, but some how not sure if it is my logic or something it continues to check the months and then the dates before returning true or false.

I want it only check the month if the year is false and only check the day if the month is false.

fun is_older(year1 : int,  month1 : int, day1 : int, year2 : int, month2 : int, day2 : int) =
if year1 < year2 andalso year1 > 0
then true
else
    if month1 < month2 andalso month1 > 0 andalso month2 <= 12
    then true
else
    if day1 < day2 andalso day1 > 0 andalso day2 <= 31
    then true

else false;

Upvotes: 1

Views: 703

Answers (1)

jimmu
jimmu

Reputation: 1056

I'm assuming you're trying to compare two dates in a year, and return true/false value. What you did is mostly correct. In your second if statement, you want to check if month1<month2 only if year1=year2. Otherwise,even if year1=2014, and year2=2013, you'll get true value if their month agree with your second if statement.

Similarly, in your third if statement, you want to check days only if year1=year2 andalso month1=month2.

Upvotes: 1

Related Questions