检查结构实例是否包含 DrRacket 中的某些值的 Racket 函数

Racket function that checks if an instance of a struct consists of certain values in DrRacket

嗨,我正在尝试在 Racket 中编写一个函数(如 Scheme/Lisp)来检查基于时间的结构实例是否仅包含 am/AM 而不是 pm/PM .

我尝试检查 AM/PM 的实例是“AM”还是“am”:

(define (isAM? AM/PM-meridiem)
  (if (or(equal? AM/PM-meridiem "AM")
         (equal? AM/PM-meridiem "am"))
      (#t))
  (else (#f)))

(define (time) (AM/PM 2 20 "am"))
         
 (define (isAM?2 time)
    (or (equal? (AM/PM-meridiem time) "AM")
        (equal? (AM/PM-meridiem time) "am")))

 (define (isAM?3 time)
    (or (equal? AM/PM-meridiem "AM")
        (equal? AM/PM-meridiem "am")))

这是 isAM 的两个结构函数?函数使用我写的:

#lang slideshow

(struct 24HourClock (hours minutes)
  #:transparent
  #:guard (lambda (hours minutes err_name)
    (if (not (and (>= hours 0) (< hours 24)))
     (error err_name "Bad hours")
    (if (not (and (>= minutes 0) (< minutes 60)))
     (error err_name "Bad minutes")
    (values hours minutes)))))

(struct AM/PM 24HourClock (meridiem)
  #:transparent
  #:guard (lambda (hours minutes meridiem err_name)
            (if (not(and(and(number? hours)(> hours 0)(<= hours 12)(number? minutes))))
                (error err_name "Hours must be between 1 and 12 : 0")
                    (if [not (and(string? meridiem)(or
                         (equal? meridiem "am")
                         (equal? meridiem "AM")
                         (equal? meridiem "pm")
                         (equal? meridiem "PM")))]                    
                    (error err_name "Invalid value for meridiem")
                    (values hours minutes meridiem)))))

看来您正在努力学习语法基础知识。

(#t) 尝试将 #t 用作过程,但事实并非如此。
(括号不是 block-structuring 某些其他语言中花括号形式的标点符号。)

而且,正如您似乎已经知道的那样,条件语句没有“else”——语法是 (if condition true-expression false-expression),正如您在 guards 中所写的那样。

第三,参数应该是你的结构的一个实例 - AM/PM-meridiem 是你需要应用到它的选择器过程。

你似乎也有用布尔值编写不必要的条件的习惯。
您也应该改掉使用您熟悉的其他语言的习惯。

这是更正后的版本:

(define (is-AM? time)
    (or (equal? (AM/PM-meridiem time) "AM")
        (equal? (AM/PM-meridiem time) "am")))

顺便说一句,C++(或 Java)中的等价物看起来像这样:

bool is_AM(AMPM time)
{
    return time.meridiem == "AM" || time.meridiem == "am";
}

并在 Python、

def is_AM(time):
{
    return time.meridiem == "AM" or time.meridiem == "am";
}