Created
July 5, 2013 12:37
-
-
Save jbpotonnier/5934255 to your computer and use it in GitHub Desktop.
Atelier Haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Friends where | |
data Relation = Friend | Cousin | Professor deriving (Show, Eq) | |
data Person = Person String deriving (Show, Eq) | |
data Fact = Fact Relation (Person, Person) deriving (Show, Eq) | |
data Graph = Graph [Fact] deriving Show | |
areFriends :: Graph -> Person -> Person -> Bool | |
areFriends (Graph facts) person1 person2 = | |
(not . null) [(p1, p2) | Fact Friend (p1, p2) <- facts, p1 == person1 && p2 == person2] | |
insertFriends :: Person -> Person -> Graph -> Graph | |
insertFriends person1 person2 (Graph facts) = | |
Graph (facts ++ [Fact Friend (person1, person2), Fact Friend (person2, person1)]) | |
emptyGraph :: Graph | |
emptyGraph = Graph [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Test.HUnit | |
import Friends (Person (..), areFriends, insertFriends, emptyGraph) | |
test1 = TestCase $ do | |
let stephan = Person "Stephan" | |
let jb = Person "Jean-Baptiste" | |
let bruno = Person "Bruno" | |
let graph = ((insertFriends bruno jb) . | |
(insertFriends jb stephan)) emptyGraph | |
assertBool "Stephan and JB should be friend" (areFriends graph stephan jb) | |
assertBool "JB and Stephan should be friend" (areFriends graph jb stephan) | |
assertBool "Bruno and JB should be friend" (areFriends graph bruno jb) | |
assertBool "Bruno should not be friend with Stephan" (not (areFriends graph stephan bruno)) | |
tests = test [ | |
TestLabel "test1" test1 | |
] | |
main = runTestTT tests |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment