-- FP1 lab exercises, 1/2 and 2/2

-- Exercise 10.

-- The formula for the roots of a quadratic is

-- (-b +- sqrt(b^2 - 4ac)(2a)

-- Define functions smallerRoot, largerRoot
-- which return the smaller and larger root of a quadratic.
-- The coefficients a,b,c shall be given as floats and the result
-- shall be a float. 

-- You may assume that the quadratic does have roots
-- (b^2 - 4ac <= 0) and is not degenerate (a /= 0). 

-- Define variants of these functions that expect the coefficients a,b,c,
-- to be given as integers.

-- Solution of first part:

smallerRoot :: Float -> Float -> Float -> Float
smallerRoot a b c = ((-b) - sqrt (b^2 - 4*a*c))/(2*a)

largerRoot :: Float -> Float -> Float -> Float
largerRoot a b c = ((-b) + sqrt (b^2 - 4*a*c))/(2*a)

-- Solution of second part:

smallerRoot' :: Int -> Int -> Int -> Float
smallerRoot' a b c = smallerRoot (fromIntegral a) (fromIntegral b) (fromIntegral c)

largerRoot' :: Int -> Int -> Int -> Float
largerRoot' a b c = largerRoot (fromIntegral a) (fromIntegral b) (fromIntegral c)


-- Exercise 15. 

-- Define a function that tests whether a character is a digit,
-- that is, one of the characters '0',...,'9'.

isDigit :: Char -> Bool
isDigit c = fromEnum '0' <= fromEnum c  &&  fromEnum c <= fromEnum '9'


{-
*Main> smallerRoot 1.5 2 3.5
NaN
*Main> smallerRoot 1.5 2 (-3.5)
-2.3333333
*Main> largerRoot 1.5 2 (-3.5)
1.0
*Main> smallerRoot' 1 2 (-3)
-3.0
*Main> largerRoot' 1 2 (-3)
1.0
*Main> isDigit 'a'
False
*Main> isDigit '8'
True
*Main>           
-}

-- Comment: the expression smallerRoot 1.5 2 3.5 yields
-- NaN ("Not a Number") because for a = 1.5, b = 2, c = 3.5,
-- b^2 - 4ac is negative, hence the square root doesn't exist
-- (in the real numbers) and therefore the quadratic has no solution.
