number_in_month exercise (Tycon Mismatch Error In SML function which tries to build a list)

本小妞迷上赌 提交于 2021-01-29 10:10:08

问题


(* 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!