How to calculate the breakeven of two contracts in Racket

As part of the Racket assignment, I have a task that I am not getting at all. Since this is homework, I would rather not get a solution directly to avoid any plagiarism issues, but would rather try to write code with your help.

Given two phone contracts, implement a function that calculates how many SMS messages one contract gets more expensive than the other. If one contract is always cheaper than another (no matter how many SMS are sent), return false.

The contracts describe my base fee, a monthly fee that can change for the second year of the contract (contracts always last two years), SMS fees and the number of free SMS that the contract includes. Therefore, I wrote my contract structure as follows:

(define-struct contract
   (basefee monthlyfee1 monthlyfee2 smsfee freesms))

In advance, I implemented two functions. One, to calculate the total cost of the contract regarding possible free sms and a given number of sms:

(define (contract-total ctr sms)
  (cond
    [(= (contract-freesms ctr) 0) (+ (contract-basefee ctr)
                                     (* 12 (contract-monthlyfee1 ctr))
                                     (* 12 (contract-monthlyfee2 ctr))
                                     (* (contract-smsfee ctr) sms))]
    [(>= (contract-freesms ctr) sms) (+ (contract-basefee ctr)
                                        (* 12 (contract-monthlyfee1 ctr))
                                        (* 12 (contract-monthlyfee2 ctr)))]
    [(<= (contract-freesms ctr) sms) (+ (contract-basefee ctr)
                                        (* 12 (contract-monthlyfee1 ctr))
                                        (* 12 (contract-monthlyfee2 ctr))
                                        (* (- sms (contract-freesms ctr))
                                           (contract-smsfee ctr)))]))

Two, compare two contracts and return the boolean value, whether the first contract will be cheaper than the second with a given number of SMS:

(define (cheaper? ctr1 ctr2 sms)
  (< (contract-total ctr1 sms)
     (contract-total ctr2 sms)))

Now I'm trying to implement the very perfect function that tells me how many SMS the second contract will be cheaper than the first. Function Signature

Contract Agreement → MaybeSMS

, , cond-clause, , ( 0 SMS, ).

? , cond-clauses ( ) ? -, sms, , .

+4
1

s, 1, 2, 3...

(cheaper? ctr1 ctr2 s)

cheaper? true false ( ).

+2

Source: https://habr.com/ru/post/1692090/


All Articles