我有一个名为的函数month_range
,它需要一年中的两天作为输入(例如,65 和 128,假设一年有 365 天)并返回一个列表,其中包含从 day1 到 day2 的天数所属的月份数。
列表的大小必须为“day2 - day1 + 1”。
示例:month_range(25,35) 应返回:[1,1,1,1,1,1,1,2,2,2,2]
我写了以下代码
fun month_range (day1:int,day2:int) =
let
val month_days= [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
fun what_month(day :int) =
let
fun aux(sum :int, numbers: int list) =
let
val numbers_tail = tl numbers
in
if sum <= (hd numbers)
then 1
else
1 + aux(sum, (hd numbers + hd numbers_tail)
:: (tl numbers_tail))
end
in
aux(day, month_days)
end
in
if (day1>day2)
then []
else what_month(day1) @ what_month(day1 + 1)@::what_month(day2)
end
但它给了我以下错误
/tmp/emacs-region5156f3r:21.51-21.54 Error: unbound variable or constructor: @::
/tmp/emacs-region5156f3r:21.12-21.70 Error: operator is not a function [literal]
operator: int
in expression:
(what_month (day1 + 1)) <errorvar>
/tmp/emacs-region5156f3r:21.12-21.70 Error: operator and operand don't agree [literal]
operator domain: 'Z list * 'Z list
operand: int * _
in expression:
what_month day1 @ (((what_month <exp>) <errorvar>) what_month) day2
uncaught exception Error
raised at: ../compiler/TopLevel/interact/evalloop.sml:66.19-66.27
../compiler/TopLevel/interact/evalloop.sml:44.55
../compiler/TopLevel/interact/evalloop.sml:296.17-296.20