问题
(* Write a function 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. The returned list should contain dates in the order they were originally given. *)
fun dates_in_months( datelist : (int*int*int) list, month : int) =
if null(tl (datelist))
then if #2(hd (datelist)) = month then #2(hd (datelist)) :: [] else []
else if #2(hd (datelist)) = month
then #2(hd (datelist)) :: number_in_month(tl datelist, month)
else number_in_month(tl datelist, month)
This is the error I get:
hw1.sml:55.22-55.78 Error: operator and operand do not agree [tycon mismatch]
operator domain: int * int list
operand: int * int
in expression:
(fn {2=<pat>,...} => 2) (hd datelist) ::
number_in_month (tl datelist,month)
val it = () : unit
Any help appreciated.
回答1:
Found the correct way:
fun dates_in_month(datelist : (int*int*int) list, month : int) =
if null(tl (datelist))
then if #2(hd (datelist)) = month
then (hd (datelist)) :: []
else []
else if #2(hd (datelist)) = month
then (hd (datelist)) :: dates_in_month(tl datelist, month)
else dates_in_month(tl datelist, month)
I had several flaws, I was calling the function I copied code over from instead of the function itself + some other syntax issues. In addition, I added just the month element of the date-tuple to the list.
来源:https://stackoverflow.com/questions/60158621/number-in-month-exercise-tycon-mismatch-error-in-sml-function-which-tries-to-bu