func SetDifferenceAndIntersection(list *Pair, moreLists ...*Pair) (difference, intersection *Pair) {
	everyNil := true
	for _, l := range moreLists {
		if l != nil {
			everyNil = false
			break
		}
	}
	if everyNil {
		return list, nil
	}
	for _, l := range moreLists {
		if list == l {
			return nil, list
		}
	}
	lists := Tabulate(len(moreLists), func(i int) interface{} { return moreLists[i] })
	return list.Partition(func(element interface{}) bool {
		return !lists.Any(func(list interface{}) bool {
			return list.(*Pair).Member(element) != nil
		})
	})
}
SetDifferenceAndIntersection returns two values -- the difference (as if by SetDifference) and
the intersection (as if by SetIntersection) of the lists. It can be implemented more efficiently
than calling SetDifference and SetIntersection separately.

Either of the answer lists may share a common tail with the first list. This operation essentially
partitions the first list.
package list
func lset2le(list1, list2 *Pair) bool {
	return list1.Every(func(x interface{}) bool {
		return list2.Member(x) != nil
	})
}
func SetLessThanEqual(lists ...*Pair) bool {
	if len(lists) < 2 {
		return true
	}
	for index, s1 := range lists[:len(lists)-1] {
		s2 := lists[index+1]
		if s1 != s2 && !lset2le(s1, s2) {
			return false
		}
	}
	return true
}
func SetEqual(lists ...*Pair) bool {
	if len(lists) < 2 {
		return true
	}
	for index, s1 := range lists[:len(lists)-1] {
		s2 := lists[index+1]
		if s1 != s2 && !(lset2le(s1, s2) && lset2le(s2, s1)) {
			return false
		}
	}
	return true
}
func (list *Pair) Adjoin(elements ...interface{}) *Pair {
	for _, element := range elements {
		if list.Member(element) == nil {
			list = &Pair{Car: element, Cdr: list}
		}
	}
	return list
}
func SetUnion(lists ...*Pair) *Pair {
	return Tabulate(len(lists), func(i int) interface{} {
		return lists[i]
	}).Reduce(func(temp, list interface{}) interface{} {
		t := temp.(*Pair)
		l := list.(*Pair)
		if l == nil {
			return t
		}
		if t == nil {
			return l
		}
		if l == t {
			return t
		}
		return l.Fold(func(temp, element interface{}) interface{} {
			if temp.(*Pair).Any(func(x interface{}) bool { return x == element }) {
				return temp
			}
			return NewPair(element, temp)
		}, t)
	}, Nil()).(*Pair)
}
func NSetUnion(lists ...*Pair) *Pair {
	return Tabulate(len(lists), func(i int) interface{} {
		return lists[i]
	}).Reduce(func(temp, list interface{}) interface{} {
		t := temp.(*Pair)
		l := list.(*Pair)
		if l == nil {
			return t
		}
		if t == nil {
			return l
		}
		if l == t {
			return t
		}
		return l.PairFold(func(temp interface{}, pair *Pair) interface{} {
			element := pair.Car
			if temp.(*Pair).Any(func(x interface{}) bool { return x == element }) {
				return temp
			}
			pair.Cdr = temp
			return pair
		}, t).(*Pair)
	}, Nil()).(*Pair)
}
func SetIntersection(list *Pair, moreLists ...*Pair) *Pair {
	lists := NAppendTabulate(len(moreLists), func(i int) *Pair {
		l := moreLists[i]
		if l == list {
			return nil
		}
		return &Pair{Car: l, Cdr: Nil()}
	})
	if lists.Any(IsNilPair) {
		return nil
	}
	if lists == nil {
		return list
	}
	return list.Filter(func(x interface{}) bool {
		return lists.Every(func(list interface{}) bool {
			return list.(*Pair).Member(x) != nil
		})
	})
}
func NSetIntersection(list *Pair, moreLists ...*Pair) *Pair {
	lists := NAppendTabulate(len(moreLists), func(i int) *Pair {
		l := moreLists[i]
		if l == list {
			return nil
		}
		return &Pair{Car: l, Cdr: Nil()}
	})
	if lists.Any(IsNilPair) {
		return nil
	}
	if lists == nil {
		return list
	}
	return list.NFilter(func(x interface{}) bool {
		return lists.Every(func(list interface{}) bool {
			return list.(*Pair).Member(x) != nil
		})
	})
}
func SetDifference(list *Pair, moreLists ...*Pair) *Pair {
	lists := NAppendTabulate(len(moreLists), func(i int) *Pair {
		l := moreLists[i]
		if l == nil {
			return nil
		}
		return &Pair{Car: l, Cdr: Nil()}
	})
	if lists == nil {
		return list
	}
	if lists.Member(list) != nil {
		return nil
	}
	return list.Filter(func(x interface{}) bool {
		return lists.Every(func(list interface{}) bool {
			return list.(*Pair).Member(x) == nil
		})
	})
}
func NSetDifference(list *Pair, moreLists ...*Pair) *Pair {
	lists := NAppendTabulate(len(moreLists), func(i int) *Pair {
		l := moreLists[i]
		if l == nil {
			return nil
		}
		return &Pair{Car: l, Cdr: Nil()}
	})
	if lists == nil {
		return list
	}
	if lists.Member(list) != nil {
		return nil
	}
	return list.NFilter(func(x interface{}) bool {
		return lists.Every(func(list interface{}) bool {
			return list.(*Pair).Member(x) == nil
		})
	})
}
func SetXor(lists ...*Pair) *Pair {
	return Tabulate(len(lists), func(i int) interface{} {
		return lists[i]
	}).Reduce(func(ai, bi interface{}) interface{} {
		a, b := ai.(*Pair), bi.(*Pair)
		ab, aintb := SetDifferenceAndIntersection(a, b)
		if ab == nil {
			return SetDifference(b, a)
		}
		if aintb == nil {
			return Append(b, a)
		}
		return b.Fold(func(tmp, xb interface{}) interface{} {
			if aintb.Member(xb) != nil {
				return tmp
			}
			return NewPair(xb, tmp)
		}, ab)
	}, Nil()).(*Pair)
}
func NSetXor(lists ...*Pair) *Pair {
	return Tabulate(len(lists), func(i int) interface{} {
		return lists[i]
	}).Reduce(func(ai, bi interface{}) interface{} {
		a, b := ai.(*Pair), bi.(*Pair)
		ab, aintb := NSetDifferenceAndIntersection(a, b)
		if ab == nil {
			return NSetDifference(b, a)
		}
		if aintb == nil {
			return NAppend(b, a)
		}
		return b.PairFold(func(tmp interface{}, bpair *Pair) interface{} {
			if aintb.Member(bpair.Car) != nil {
				return tmp
			}
			bpair.Cdr = tmp
			return bpair
		}, ab)
	}, Nil()).(*Pair)
}