diff --git a/WhyHaskellMatters.iml b/WhyHaskellMatters.iml new file mode 100644 index 0000000..136722b --- /dev/null +++ b/WhyHaskellMatters.iml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/AlgebraicDataTypes.hs b/src/AlgebraicDataTypes.hs index b34c4b8..09db163 100644 --- a/src/AlgebraicDataTypes.hs +++ b/src/AlgebraicDataTypes.hs @@ -6,7 +6,7 @@ import Control.Monad ((>=>)) data Status = Green | Yellow | Red deriving (Eq, Show) -- a simple product type -data Pair a b = Tuple a b deriving (Show) +data Pair a b = P a b deriving (Show) data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Show) @@ -40,12 +40,12 @@ findDivRoot' x key map = safeRoot d findDivRoot'' x key map = - findValue key map >>= - (safeDiv x >=> + findValue key map >>= + (safeDiv x >=> safeRoot) findDivRoot''' x key map = do y <- findValue key map d <- safeDiv x y safeRoot d - \ No newline at end of file + diff --git a/src/LazyEvaluation.hs b/src/LazyEvaluation.hs index c6f963b..4cd5b58 100644 --- a/src/LazyEvaluation.hs +++ b/src/LazyEvaluation.hs @@ -36,6 +36,11 @@ primes = sieve (2:[3,5..]) wenn :: Bool -> b -> b -> b wenn p x y = if p then x else y + +cond :: [(Bool, a)] -> [a] +cond [] = [] +cond ((True, v):rest) = v : cond rest +cond ((False, _):rest) = cond rest diff --git a/test/Tests/AllTestsSpec.hs b/test/Tests/AllTestsSpec.hs index f7095bc..031e12d 100644 --- a/test/Tests/AllTestsSpec.hs +++ b/test/Tests/AllTestsSpec.hs @@ -4,9 +4,18 @@ import Test.Hspec import Test.QuickCheck import Functions +import LazyEvaluation +import AlgebraicDataTypes spec :: Spec spec = describe "Why Haskell matters" $ do it "it has functions" $ - property $ \x -> square x `shouldBe` x*x + property $ \x -> square x `shouldBe` x*x + it "can handle user defined controll structures" $ + cond [(False, viciousCircle), + (True, 3), + (True, 5), + (False, viciousCircle)] `shouldBe` [3,5] + it "can build trees" $ + show (Node (Leaf 1) (Node (Leaf 2) (Leaf 3))) `shouldBe` "Node (Leaf 1) (Node (Leaf 2) (Leaf 3))" \ No newline at end of file