Tre*_*ink 0 scheme if-statement conditional-statements racket
我目前正在Scheme中为一个作业编写一个小项目.我没有长时间使用Scheme,所以我对语法不够强.
问题是在if句中使用"和".我有一个日历中的约会列表,但我只想要在一定时间间隔之间的约会.因此,我需要检查开始和结束时间.
我希望实现的内容在C#中看起来像这样:
List<appointment> appointments = new List<appointment>();
foreach (appointment app in calendar) {
if(app.getstart() >= from-time && app.getend() <= to-time) {
appoinments.add(app);
}
}
Run Code Online (Sandbox Code Playgroud)
我目前在Scheme中拥有的是:
(define (time-calendar cal from-time to-time)
(map (lambda (app)(if (> from-time (send 'getstart app)) #t #f))
(send 'getappointments cal)))
Run Code Online (Sandbox Code Playgroud)
采用日历"cal"和时间间隔(从时间到时间)然后我从cal获得约会(app)并迭代它们.对于每个人,我检查时间是否大于"app"的开始时间.相应地返回true或false.这很有效,但我仍然需要考虑约会是否也在"准时"之前结束.这应该是添加另一个条件的简单问题,但我根本无法使其工作.
任何人都可以帮助我检查第二个变量的正确语法吗?我知道关于Racket文档,但我仍然无法解决我的问题.
我尝试更换cond的if句子.
我也尝试了一些"and"部分的变体,与此类似,但无法正确使用语法:
(define (time-calendar cal from-time to-time)
(map (lambda (app)(if (and((> from-time (send 'getstart app))) (< to-time (send 'getend app))) #t #f))
(send 'getappointments cal)))
Run Code Online (Sandbox Code Playgroud)
(and expression1 expression2 ...)
Run Code Online (Sandbox Code Playgroud)
过多的括号被理解为您的表达式应用于过程.例如.
(and ((if some-var + -) 4 6))
Run Code Online (Sandbox Code Playgroud)
这里的结果是-2或10,具体取决于some-var的值.这and是多余的,因为(and x)它是相同的x.
至于你的代码,它应该是这样的:
(define (time-calendar cal from-time to-time)
(map (lambda (app)
(and (> from-time (send 'getstart app))
(< to-time (send 'getend app))))
(send 'getappointments cal)))
Run Code Online (Sandbox Code Playgroud)
这if是多余的,因为and评估#f其中一个是否为假.>并<始终评估为#f或#t.即使它没有,你可以使用除#f真实之外的所有数据,因此在大多数情况下额外if是多余的.如果你还需要其他表达式返回的东西,那么整个和表达式都是谓词.现在,因为>你需要许多参数,实际上你不需要and:
(define (time-calendar cal from-time to-time)
(map (lambda (app)
(> from-time (send 'getstart app) to-time))
(send 'getappointments cal)))
Run Code Online (Sandbox Code Playgroud)