MA0301/exercise5/scripts/ex7.hs

30 lines
990 B
Haskell
Raw Permalink Normal View History

2021-03-24 11:48:48 +01:00
import Control.Monad (mapM_)
import Data.List (nub)
type Pair = (Integer, Integer)
type Relation = (Pair, Pair)
cartesianProduct :: [Integer] -> [Pair]
cartesianProduct domain = [ (x,y) | x <- domain,
y <- domain ]
calculateRelations :: [Pair] -> (Pair -> Pair -> Bool) -> [Relation]
calculateRelations set relation
= nub [ (p1, p2) | p1 <- set,
p2 <- set,
relation p1 p2 ]
getRelatedPairsOf :: Pair -> [Relation] -> [Pair]
getRelatedPairsOf s r = [ p2 | (p1, p2) <- r,
p1 == s ]
main :: IO ()
main = do
let
-- set a be the cartesian product of two lists of integers from 1 to and including 5
setA = cartesianProduct [1..5]
-- set r be the relation on a that satisfy the following condition
r = calculateRelations setA (\(x,y) (u,v) -> x+y == u+v)
-- filter out the equivalence classes of the following pairs
mapM_ (print . flip getRelatedPairsOf r) [(1,3), (2,4), (1,1)]