-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.hs
43 lines (39 loc) · 1.68 KB
/
calculator.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import Text.Read (readMaybe)
-- Function to perform calculations
calculate :: Double -> Double -> String -> Either String Double
calculate num1 num2 operation =
case operation of
"+" -> Right (num1 + num2)
"-" -> Right (num1 - num2)
"*" -> Right (num1 * num2)
"/" -> if num2 == 0
then Left "Error: Division by zero"
else Right (num1 / num2)
_ -> Left "Error: Unsupported operation"
-- Main function
main :: IO ()
main = do
putStrLn "Simple Calculator"
let loop = do
putStr "Enter first number: "
input1 <- getLine
case readMaybe input1 :: Maybe Double of
Nothing -> putStrLn "Invalid number. Please try again." >> loop
Just num1 -> do
putStr "Enter operation (+, -, *, /): "
operation <- getLine
putStr "Enter second number: "
input2 <- getLine
case readMaybe input2 :: Maybe Double of
Nothing -> putStrLn "Invalid number. Please try again." >> loop
Just num2 -> do
let result = calculate num1 num2 operation
case result of
Left errMsg -> putStrLn errMsg
Right res -> putStrLn $ "Result: " ++ show res
putStrLn "Do you want to perform another calculation? (yes/no)"
continue <- getLine
if continue == "yes"
then loop
else putStrLn "Goodbye!"
loop