SML programming to racket programming

Viewed 44

Using SML, I have completed a problem called dates_in_month that takes a list of dates and a month (i.e., an int) and returns a list holding the dates from the argument list of dates that are in the month. While it's done and easy in SML, my question is how this would I write the same program in the Racket programming language, where functions and lists are defined and used differently?

An example is (dates_in_month 5 [(2002 05 12) (1999 05 12) (1980 12 20)]), and the output should be (2002 05 12) (1999 05 12) because those are the dates where the months is 5.

1 Answers

This is relatively straightforward with filter:

(define (dates_in_month n l)
  (filter (lambda (date) (= (cadr date) n)) l))

then

> (dates_in_month 5 '[(2002 5 12) (1999 5 12) (1980 12 20)])
'((2002 5 12) (1999 5 12))
>
Related