cocoakrispies93
cocoakrispies93

Reputation: 61

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

Hi I'm trying to write a function in Racket (like Scheme/Lisp) to check if an instance of a struct based on time consists of only am/AM and not pm/PM.

Here are my attempts at checking to see if an instance of AM/PM is just "AM" or just "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")))

Here's the two struct functions the isAM? function uses that I wrote:

#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)))))

Upvotes: 0

Views: 533

Answers (1)

molbdnilo
molbdnilo

Reputation: 66459

It looks like you're struggling with the fundamentals of syntax.

(#t) tries to use #t as a procedure, which it isn't.
(Parentheses are not block-structuring punctuation in the manner of curly braces in some other languages.)

And, as you seem to already know, a conditional does not have an "else" - the syntax is (if condition true-expression false-expression), as you wrote in the guards.

And third, the parameter should be an instance of your struct - AM/PM-meridiem is the selector procedure you need to apply to it.

You also seem to have a habit of writing unnecessary conditionals with boolean values.
You should drop that habit in those other languages you are familiar with, as well.

Here is a corrected version:

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

As an aside, the equivalent in C++ (or Java) would look something like this:

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

and in Python,

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

Upvotes: 2

Related Questions