我开始学习标准ML Programming languages当然。SML和功能性编码风格
在第一个作业中,我尝试编写一个函数is_older
,该函数需要两个日期并且计算结果为true
或false
。如果第一个参数是第二个参数之前的日期,则它的计算结果为true
(如果两个日期相同,则结果为false
。)。
所以我写了下面的代码:
fun is_older(first: int * int * int, second: int * int * int) =
if(#1 first = #1 second andalso #2 first = #2 second andalso #3 first = #3 second) then false
else if (#1 first < #1 second) then true
else if (#1 first = #1 second andalso #2 first < #2 second) then true
else if (#1 first = #1 second andalso #2 first = #2 second andalso #3 first < #3 second) then true
else false
的代码工作正常,但它看起来丑陋。
如何以功能样式重写此代码?
添加一些评论将帮助其他人了解你的答案propposal 。 – Yaroslav