'Using Racket BSL to create the following Boolean functions with restrictions

Restrictions for functions:

  • Use Racket BSL
  • Not allowed to use logical operators
  • Can only use if, the names of parameters, #true, and #false (though all may not be needed)
  • cover tests for ALL possible input combinations for the parameters
  • not allowed to use an if that takes the following form (if parameter #true #false)

This is what I have tried so far (but I used logical operators (and, not, or) which is not allowed:

; same? : Boolean Boolean -> Boolean
; returns true if either both boolean
; parameters are true or both are false

(check-expect (same? #t #t) #t)
(check-expect (same? #t #f) #f)
(check-expect (same? #f #t) #f)
(check-expect (same? #f #f) #t)

(define (same? x y)
  (or (and x y) (not (or x y))))

; TODO: Design the function non-agreement? that takes two Boolean parameters
;       and returns true if at least one of them is false.

; non-agreement? : Boolean Boolean -> Boolean
; returns true if at least one of the boolean
; parameters is false

(check-expect (non-agreement? #t #t) #f)
(check-expect (non-agreement? #t #f) #t)
(check-expect (non-agreement? #f #t) #t)
(check-expect (non-agreement? #f #f) #t)

(define (non-agreement? x y)
  (not (and x y))

; TODO: Design the function follow-directions that takes two Boolean parameters:
;       * if the first is false, it simply returns the second;
;       * if the first is true, it returns the opposite of the second

; follow-directions : Boolean Boolean -> Boolean
; returns the second boolean parameter if the first
; is false; otherwise, returns the opposite of the
; second boolean parameter if the first is true

(check-expect (follow-directions #t #t) #f)
(check-expect (follow-directions #t #f) #t)
(check-expect (follow-directions #f #t) #t)
(check-expect (follow-directions #f #f) #f)

(define (follow-directions x y)
  (or (and x (not y)) (and (not x) y)))


Solution 1:[1]

The restriction on using if in the form of (if parameter #true #false) can be lifted by using the values of your parameters instead.

E.g. If x = #true, after evaluating x an if-statement the code will only run the then-branch. In this branch I am certain x = #true so it can be substituted everywhere (in this branch) I need #true. If x = #false then I can substitute #false with x in the else-branch.

(define (same? x y)
  (if x
      y  ; = (if y #true #false)
      (if y 
          x ; = #false
          #true)))

(define (non-agreement? x y)
  (if x
      (if y 
          #false
          x) ; = #true
      #true))

(define (follow-directions x y)
  (if x
      (if y
          #false
          x) ; = #true
      y))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Laeremadr