Search code examples
mathschemeequivalence

Scheme equivalence relation


I need help defining a Scheme function that takes a partition (list of lists) and returns a list of pairs that represents an equivalence relation. I've started writing some code but I'm realizing I'm going in the wrong direction. I've been looking at this for hours with no progress.

(define partition 
  (lambda (piv l p1 p2)
    (if (null? l) 
        (list p1 p2)
        (if (< (car l) piv)
            (partition piv (cdr l) (cons (car l) p1) p2)
            (partition piv (cdr l) p1 (cons (car l) p2))))))

However, the function needs to work in a way such that (partition ’((3 4) (5))) return ((3 3) (3 4) (4 3) (4 4) (5 5))

Any help would be greatly appreciated


Solution

  • Another example would be helpful. Going with the assumption that all the elements of a member list are equivalent to each other, this might suffice:

    (define (partition lol) ; lol, list of lists
        (apply append
            (map
                (lambda (liszt)
                    (apply append (map
                        (lambda (elem)
                            (map
                                (lambda (other)
                                    (list elem other))
                                liszt))
                        liszt)))
                lol)))