- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Java Program to Make a Simple Calculator Using switch...case
- Golang Program to make a Simple Calculator using Switch Case
- C++ Program to Make a Simple Calculator to Add, Subtract, Multiply or Divide Using switch...case
- Java program to generate a calculator using the switch case
- C/C++ program to make a simple calculator?
- Switch case calculator in JavaScript
- How to write a simple calculator program using C language?
- Create a simple calculator using Java Swing
- Simple Calculator using TCP in Java
- Menu Driven C++ Program for a Simple Calculator
- Simple GUI calculator using Tkinter in Python
- Haskell Program to create a simple recursive function
- Creating a Simple Calculator using HTML, CSS, and JavaScript
- How to build a simple GUI calculator using tkinter in Python
- Write a C program of library management system using switch case
