[Added exercise about geometric mean. Nils Anders Danielsson **20050831205349] { hunk ./Mean.hs 3 -mean2 x y = (x + y)/2 +mean x y = (x + y)/2 hunk ./Mean.hs 7 - (min x y <= mean2 x y) && (mean2 x y <= max x y) + (min x y <= mean x y) && (mean x y <= max x y) + +geometricMean :: Floating a => a -> a -> a +geometricMean x y = sqrt (x * y) + +prop_geometricMeanBetween :: Double -> Double -> Property +prop_geometricMeanBetween x y = + x > 0 && y > 0 ==> + (min x y <= geometricMean x y) && (geometricMean x y <= max x y) + +prop_geometricMeanBetween' :: Double -> Double -> Bool +prop_geometricMeanBetween' x y = + (min x y <= geometricMean x y) && (geometricMean x y <= max x y) hunk ./MoreFunctions.ws 5 +geometricMeanURL = "http://en.wikipedia.org/wiki/Geometric_mean" + hunk ./MoreFunctions.ws 10 - h2 $ text "More on functions" + h2 $ text "More functions" hunk ./MoreFunctions.ws 128 + + <#>Repeat the procedure above, but now with the >geometric + mean instead of the arithmetic one. ?> do + + fixed $ unlines + [ "geometricMean :: Floating a => a -> a -> a" + , "geometricMean x y = sqrt (x * y)" + ] + + fixed $ unlines + [ "prop_geometricMeanBetween :: Double -> Double -> Bool" + , "prop_geometricMeanBetween x y =" + , " (min x y <= geometricMean x y) && (geometricMean x y <= max x y)" + ] + + p <#>Let us try out the property. Several things can happen: + + prompts "Main>" + [ ( "quickCheck prop_geometricMeanBetween" + , unlines [ "Falsifiable, after 2 tests:" + , "-0.2" + , "-0.4" + ] + ) + ] + + prompts "Main>" + [ ( "quickCheck prop_geometricMeanBetween" + , unlines [ "3" + , "Program error: argument out of range" + ] + ) + ] + + <#>What is the problem? ?> do + + p <#>The geometric mean is only defined for positive numbers. We + can restrict the test to positive numbers by using (==>). + + fixed $ unlines + [ "prop_geometricMeanBetween :: Double -> Double -> Property" + , "prop_geometricMeanBetween x y =" + , " x > 0 && y > 0 ==>" + , " (min x y <= geometricMean x y) && (geometricMean x y <= max x y)" + ] + + p <#>Now the test should succeed. Note that we have to change + the type of the property when using (==>). }