101k views
2 votes
Write a function (is-older date1 date2) that takes two dates and evaluates to true or false. It evaluates to true if the first argument is a date that comes before the second argument. (If the two dates are the same, the result is false.) Hint: use a let-expression to capture the different pieces of the dates.

User Rogue
by
5.8k points

2 Answers

5 votes

Final answer:

The function (is-older date1 date2) in Scheme checks if date1 comes before date2 by comparing year, month, and day components. It uses a let-expression for comparison and returns true only if date1 is strictly earlier than date2.

Step-by-step explanation:

To write a function (is-older date1 date2) that evaluates whether the first date comes before the second, you can use a programming language like Scheme. The function will utilize a let-expression to destructure the dates into their components (year, month, day) and compare them sequentially. Here is an example of how such a function might be implemented:

(define (is-older date1 date2)
(let* ((year1 (car date1))
(month1 (cadr date1))
(day1 (caddr date1))
(year2 (car date2))
(month2 (cadr date2))
(day2 (caddr date2)))
(or (< year1 year2)
(and (= year1 year2)
(or (< month1 month2)
(and (= month1 month2)
(< day1 day2))))))) )

This Scheme function will return true if date1 is older than date2, and false otherwise, including when date1 and date2 are the same.

User Savepopulation
by
6.1k points
2 votes

Answer:

The solution code is written in Python 3

  1. def is_older(date1, date2):
  2. date1_list = date1.split("/")
  3. date2_list = date2.split("/")
  4. for i in range(0, len(date1_list)):
  5. date1_list[i] = int(date1_list[i])
  6. date2_list[i] = int(date2_list[i])
  7. if(date1_list[2] > date2_list[2]):
  8. return False
  9. elif(date1_list[2] < date2_list[2]):
  10. return True
  11. else:
  12. if(date1_list[1] > date2_list[1]):
  13. return False
  14. elif(date1_list[1] < date2_list[1]):
  15. return True
  16. else:
  17. if(date1_list[0] >= date2_list[0]):
  18. return False
  19. else:
  20. return True
  21. print(is_older("2/14/2020", "2/13/2019"))
  22. print(is_older("1/15/2019","1/15/2020"))
  23. print(is_older("3/10/2020", "3/10/2020"))

Step-by-step explanation:

Let's presume the acceptable date format is (mm/dd/yyyy). We create a function is_older that takes two input dates (Line 1). Next we use split method to convert the two date strings into list of date components, month, day and year (Line 2-3).

The codes in Line 5-7 are to convert each number in the list from string to numerical type.

Next, we do the date comparison started with the year which is indexed by 2. If the year in date1_list is greater than the date2_list, return False (Line 10). If it is smaller, return True (Line 12). If it is equal, then we need to go on to compare the month component (Line 14 - 17). The comparison logic is similar to checking the year.

If the month of the two dates are the same, we go on to compare the day (Line 19 -22). If the day in date1_list is greater or equal to the date2_list, it shall return False, else it return True.

We test the function using three sample inputs (Line 24-25) and the output is as follows:

False

True

False

User Eliott
by
5.7k points