Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab 4 #3

Open
BertLisser opened this issue Oct 9, 2018 · 0 comments
Open

Lab 4 #3

BertLisser opened this issue Oct 9, 2018 · 0 comments

Comments

@BertLisser
Copy link

Ex .5

-- the symmetric closure of a set can be reached by including the reverse of each relation in the set, we do this by adding the reverse, followed by removing any duplicates and sorting these two items and the symmetric closure of the tail.
symClos :: Ord a => Rel a -> Rel a
symClos [] = []
symClos ((x,y):xs) = sort $ nub ((x,y):(y,x):(symClos xs))

Less expensive

symClos' ::  Rel a -> Rel a
symClos' [] = []
symClos' ((x,y):xs) = (x,y):(y,x):(symClos' xs)
symClos :: Ord a => Rel a -> Rel a
symClos  xs =  (sort . nub)  (symClos' xs)

Ex 6

-- helper function that finds all of the missing transitive relations in a set, one layer deep. It also sorts and removes duplicates to maintain set properties
trClosFunc :: Ord a => Rel a -> Rel a
trClosFunc a = nub $ sort (a ++ (a @@ a))

-- the transitive closure of a set can be found by finding missing layers until adding a missing layer does not change the set, because if adding the next layer equals adding the empty set, we have no more missing items to insert into the set. If no items are missing, this means we are done.
trClos :: Ord a => Rel a -> Rel a
trClos a = fp' trClosFunc a

It does a step too much. Less expensive

trClos :: Ord a => Rel a -> Rel a
trClos x= fp' trClosFunc  ((sort.nub) x)

For the readability don't take the same name for the type and argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant