【发布时间】:2013-01-17 07:50:38
【问题描述】:
我开始通过Programming languages 课程学习标准机器学习。
在第一个作业中,我尝试编写一个函数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
代码运行良好,但看起来很难看。
如何以函数式风格重写这段代码?
【问题讨论】:
标签: functional-programming sml