Skip to content

Instantly share code, notes, and snippets.

@martinsson
Created October 30, 2016 13:26
Show Gist options
  • Save martinsson/7444bd44e27a978fc5a5dabd0ad0918a to your computer and use it in GitHub Desktop.
Save martinsson/7444bd44e27a978fc5a5dabd0ad0918a to your computer and use it in GitHub Desktop.
Set dojo in Haskell (WIP)
import Test.Hspec
import Data.List
main = hspec $ do
describe "set" $ do
it "one card is not a set" $ do
containsset [(Rectangle, Full, Blue, One)] `shouldBe` False
it "all One Full Blue is a set" $ do
containsset [(Rectangle, Full, Blue, One), (Circle, Full, Blue, One), (Triangle, Full, Blue, One)] `shouldBe` True
it "all One Blue Rectangle is a set" $ do
containsset [(Rectangle, Full, Blue, One), (Rectangle, Empty, Blue, One), (Rectangle, Half, Blue, One)] `shouldBe` True
it "can work with 4 cards" $ do
containsset [(Rectangle, Full, Blue, One), (Rectangle, Empty, Blue, One), (Circle, Half, Blue, One), (Rectangle, Half, Blue, One)] `shouldBe` True
describe "remove property" $ do
it "remove the first property" $ do
removeProperty1 (Rectangle,Empty,Blue,One) `shouldBe` (Empty,Blue,One)
removeProperty1 (Rectangle,Full,Blue,One) `shouldBe` (Full,Blue,One)
it "remove the nth property" $ do
removeProperty2 (Rectangle,Full,Blue,One) `shouldBe` (Rectangle, Blue, One)
containsset :: [Card] -> Bool
containsset [_] = False
containsset cards = any (== True) [has3EqualIn4Tuple removeProperty1 , has3EqualIn4Tuple removeProperty2]
where has3equalTuples l = any (== 3) $ map length $ group $ sort l
has3EqualIn4Tuple removeNth = has3equalTuples $ map removeNth cards
removeProperty1 (_,x,y,z) = (x,y,z)
removeProperty2 (x,_,y,z) = (x,y,z)
type Card = (Shape, Shate, Color, Number)
data Shape = Rectangle| Circle | Triangle deriving (Eq, Show,Ord)
data Shate = Full | Half | Empty deriving (Eq,Show,Ord)
data Color = Blue deriving (Eq,Show,Ord)
data Number = One deriving (Eq,Show,Ord)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment