module TFwH.Chap02
(
(+++)
, sqr
, infinity
, three
, factorial
, fact
, to
, Pin
, Person(..)
, samePerson
, cwords
, roots
) where
import TFwH.CommonWords
(+++) :: Int -> Int -> Int
x +++ y = if even x then y else x + y
sqr :: Integer -> Integer
sqr x = x * x
infinity :: Integer
infinity = 1 + infinity
three :: Integer -> Integer
three x = 3
factorial :: Integer -> Integer
factorial n = fact (n, 1)
fact :: (Integer, Integer) -> Integer
fact (x, y) = if x == 0 then y else fact (x - 1, x * y)
to :: Bool -> Bool
to b = not (to b)
data Person = Person { pin :: Pin }
type Pin = Int
instance Eq Person where
x == y = pin x == pin y
samePerson :: Person -> Person -> Bool
samePerson x y = pin x == pin y
cwords :: Int -> FilePath -> FilePath -> IO ()
cwords n infile outfile
= do { text <- readFile infile
; writeFile outfile (commonWords n text)
; putStrLn "cwords done!"
}
main :: IO ()
main = do { putStrLn "Take text from where:"
; infile <- getLine
; putStrLn "How many words:"
; n <- getLine
; putStrLn "Put results where:"
; outfile <- getLine
; text <- readFile infile
; writeFile outfile (commonWords (read n) text)
; putStrLn "cwords done!"
}
roots :: (Float, Float, Float) -> (Float, Float)
roots (a, b, c)
| a == 0 = error "not quadratic"
| disc < 0 = error "complex roots"
| otherwise = ((-b - r) / e, (-b + r) / e)
where
{ disc = b * b - 4 * a * c; r = sqrt disc; e = 2 * a }