-- My fourth script


-- Conversion to ASCII code


isDigit :: Char -> Bool
isDigit c = 
    let  n = fromEnum c 
    in (48 <= n) && (n <= 57)

-- Coercion from Int to Float

half :: Int -> Float
half x = fromIntegral x / 2



-- Case analysis

-- if-then-else

max2 :: Int -> Int -> Int
max2  x y = if x >= y then x else y

-- guarded equations

sign :: Float -> Int
sign x | x > 0       =  1
       | x == 0      =  0
       | otherwise   =  -1

{-
sign x = if x > 0 then 1 else
         if x == 0 then 0 else -1
-}

-- pattern matching

first :: [Int] -> Int
first (x:_) = x 
first [] = error "first of empty string"


tail1 :: [Int] -> [Int]
tail1 []   =  []
tail1 (_:xs) = xs

-- swap first 2 elements

swap :: [a] -> [a]
swap (x:y:xs) = y:x:xs
swap zs       = zs


-- case of

empty :: [a] -> Bool
empty x =  case x of
             { []     -> True   ;
               (x:xs) -> False }

{-
empty :: [a] -> Bool
empty []     = True
empty (_:_) = False
-}



-- local definition of a function

totalArea :: Float -> Float -> Float
totalArea r s = let area x = x^2*pi
                in area r  + area s 
  

{-
totalArea r s = area r  + area s  
   where 
      area x = x^2*pi
-}
{- 
totalArea r s = r^2*pi + s^2*pi
-}

