HASKELL   26

calc

Guest on 22nd September 2022 12:28:53 AM

  1. module Calc
  2.         where
  3. -- read a + or - from the first line
  4. -- then perform operation on arguments supplied on second and third line
  5. -- and print the result
  6.  
  7. main :: IO ()
  8. main = do
  9.         op <- getLine   -- will return something of type IO String
  10.         num1 <- getLine
  11.         num2 <- getLine
  12.         if (ifplus op)  -- will use read to convert String to Int
  13.                         -- putStrLn is a function that returns an IO ()
  14.                         -- show is a function that converts Int to String
  15.                 then putStrLn ( show (read num1 + read num2) )
  16.                 else putStrLn ( show (read num1 - read num2) )
  17.  
  18. ifplus :: String -> Bool
  19. ifplus "+" = True
  20. ifplus _ = False

Raw Paste


Login or Register to edit or fork this paste. It's free.