I would like to implement this function in Racket. How could I rewrite this function in Racket?
My CODE IN PYTHON
# function to check transitive relation
def is_transitive(relation):
# for all (a, b) and (b, c) in Relation ; (a, c) must belong to Relation
for a,b in relation:
for c,d in relation:
if b == c and ((a,d) not in relation):
return False
return True
transitive?
takes a list of pairs as its only input. That list of pairs represents a binary relation. The function should return #t
if that binary relation is transitive, as illustrated below.
> (transitive? '((1 2) (2 3) (1 3)))
#t
> (transitive? '((1 3) (1 2) (2 3)))
#t
> (transitive? '((1 2) (2 3)))
#f
> (transitive? '((1 1) (1 2) (2 1) (2 2) (3 3)))
#t
> (transitive? '((1 2) (3 3) (2 2) (2 1) (1 1)))
#t
> (transitive? '((2 3) (3 3) (1 2) (1 1)))
#f
Here is what I have in Racket so far:
(define (get-all-relations-of x set)
(if (null? set)
'()
(if (equal? x (car (car set)))
(cons (cdr (car set))
(get-all-relations-of x (cdr set)))
(get-all-relations-of x (cdr set)))))
(define (exist-relation? r set)
(if (null? set)
#f
(if (equal? r (car set))
#t
(exist-relation? r (cdr set)))))
(define (exist-all-transitive-relations-of? x r set)
(if (null? r)
#t
(if (not (exist-relation? (cons x (car r)) set))
#f
(exist-all-transitive-relations-of? x (cdr r) set))))
(define (transitive? set)
(if (null? set)
#t
(if (and (not (null? (get-all-relations-of (cdr (car set)) set)))
(not (exist-all-transitive-relations-of?
(car (car set))
(get-all-relations-of (cdr (car set)) set)
set)))
#f
(transitive? (cdr set)))))
It only works when(exist-all-transitive-relations-of? x r set) is true.
Here is my output :
>(exist-all-transitive-relations-of? 1 '(2 5 6) '((1 2) (1 5) (6 8)))))
> #t
>(define (transitive? '((1 2) (2 6)))
> #f
>(define (transitive? '((1 2) (2 6)(2 7)(1 7)))
> #f
>(define (transitive? '((1 2) (2 6)(2 7)(1 7)(1 6)))
> #t
How can I modify my code? so that I don't have to test if (define (exist-all-transitive-relations-of? x r set) is true separately
get-all-relations-of
,exist-relation?
, andexist-all-transitive-relations-of?
with test cases to find out if they are behaving as expected? – ad absurdum