JoelSjogren

minimal.hs

Feb 11th, 2021
2,005
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE FlexibleInstances #-}
  2. import Data.Set (Set,elems,cartesianProduct,fromList,empty,insert,singleton,
  3.                 union,unions)
  4.  
  5. data CombinatorialProblem a = CP
  6.   {
  7.     bound :: Set a,
  8.     close :: Set a -> Set a
  9.   }
  10.  
  11. enumerate :: CombinatorialProblem a -> Set a
  12. enumerate cp = iterate' f null (empty, singleton (close cp empty))
  13.  where
  14.    f (old, new) = (union old new, fromList fresh)
  15.      where
  16.        fresh = [close cp (insert a s) | a <- elems (bound cp), s <- elems new]
  17.  
  18. iterate' :: ((a,a) -> (a,a)) -> (a -> Bool) -> (a,a) -> a
  19. iterate' f p x = let (x0,x1) = iterate'' x in x0
  20.  where
  21.    iterate'' x = if p (let (x0,x1) = x in x1) then x else iterate'' (f x)
  22.  
Advertisement