Haskell Program to Make a Simple Calculator Using switch...case


In this article we are going to learn how to make a simple calculator in Haskell using switch…case. In the first example, we are going to use case statements with different operators and in the second example, we are going to use map of functions as (fromList [('+', (+)), ('-', (-)), ('*', (*)), ('/', (/))]).

Method 1: Program to make a simple calculator using case statement

In this method, a simple calculator program in Haskell is formed which takes three arguments, x, op and y. The x and y are of type Double and op is of type Char. The calculator function takes these three arguments and uses a case statement to check the value of op. Depending on the value of op, it performs the corresponding operation on x and y and returns the result.

Algorithm

  • Step 1 − The user-defined calculator function is defined using case statements,

  • Step 2 − Program execution will be started from main function. The main() function has whole control of the program.

  • Step 3 − The variables named, “x” and “y” are being initialized to hold the operands value and “op” is initialized to hold the operator.

  • Step 4 − The result of the given operation is printed to the console using ‘print’ function after the function is called.

Example 1

In this example, we are going to see that how we can make a simple calculator by using case statements.

calculator :: Double -> Char -> Double -> Double
calculator x op y = case op of
   '+' -> x + y
   '-' -> x - y
   '*' -> x * y
   '/' -> x / y
   _   -> error "Invalid operator"

main :: IO ()
main = do
   let x = 5.0
   let op = '*'
   let y = 2.0
   print (calculator x op y)

Output

10.0

Example 2

In this example, we are going to see that how we can make a simple calculator by using case statements with toLower function.

import Data.Char (toLower)

calculator :: Double -> Double -> Char -> Double
calculator a b op = case toLower op of
   '+' -> a + b
   '-' -> a - b
   '*' -> a * b
   '/' -> a / b
   _   -> error "Invalid operator"

main :: IO ()
main = do
   let a = 2
   let b = 5
   let op = "*"
   print (calculator a b (head op))

Output

10.0

Example 3

In this example, we are going to see that how we can make a simple calculator by using map of functions.

import Data.Map (Map, fromList, lookup)

calculator :: Double -> Char -> Double -> Double
calculator x op y = case Data.Map.lookup op operators of
   Just f  -> f x y
   Nothing -> error "Invalid operator"
   where
      operators = fromList [('+', (+)), ('-', (-)), ('*', (*)), ('/', (/))]

main :: IO ()
main = do
   let x = 5.0
   let op = '*'
   let y = 2.0
   print (calculator x op y)

Output

10.0

Conclusion

A simple calculator is a device or program that performs basic mathematical operations like addition, subtraction, multiplication, and division. It usually has a user-friendly interface that allows users to input numbers and operators and obtain the results of their calculations. In Haskell, a simple calculator can be created using case statements or by using toLower function with case statements and also by using map of functions.

Updated on: 13-Mar-2023

511 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements