Skip to content

Instantly share code, notes, and snippets.

@linstantnoodles
Created October 12, 2012 01:20
Show Gist options
  • Save linstantnoodles/3876813 to your computer and use it in GitHub Desktop.
Save linstantnoodles/3876813 to your computer and use it in GitHub Desktop.
checks a relation for transitive property
//checks for existence of (b,c)
let rec exists_bc(b,r) =
match r with
[] -> false
| (x,c)::tail -> (x == b) || exists_bc(b,tail);;
//constructs the list of relations that must exist for element a
let rec create_ac(a,b,r) =
match r with
[] -> []
| (x,c)::tail -> if (x == b)
then (a,c)::create_ac(a,b,tail)
else create_ac(a,b,tail);;
//creates a list of lists
let rec createList og_set r =
match r with
[] -> []
| (a,b)::tail -> if exists_bc(b,og_set)
then create_ac(a,b,og_set)::createList og_set tail
else createList og_set tail;;
//check if relation exists in set
let rec exists(a,b,set) =
match set with
[] -> false
|(x,y)::tail -> (a == x && b == y) || exists(a,b,tail);;
//check if relations are subset of set
let rec list_exists(list,set) =
match list with
[] -> true
|(x,y)::tail -> exists(x,y,set) && list_exists(tail,set);;
//check if relations of all lists are subsets of set
let rec isTran og_set l =
match l with
[] -> true
| list::tail -> list_exists(list,og_set) && isTran og_set tail;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment