
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check Whether a Number is Positive or Negative in Haskell
In Haskell, we can Check Whether a Number is Positive or Negative by using comparison operators and if-else statements. In the first example, we are going to use (isPositive n | n > 0 = "Positive" | n == 0 = "Zero" | otherwise = "Negative") function. And in the second example, we are going to use (isPositive n = if n > 0 then "Positive" else if n == 0 then "Zero" else "Negative") function.
In the following examples, the function isPositive takes an Integer argument n, and returns a string indicating whether the number is positive, negative, or zero. The function uses a pattern match to determine if n is greater than 0, equal to 0, or less than 0. If n is greater than 0, the function returns the string "Positive". If n is equal to 0, it returns the string "Zero". If n is less than 0, it returns the string "Negative".
Algorithm
Step 1 ? The isPositive function is defined as,
For example 1 & 2 ?
| n > 0 = "Positive" | n == 0 = "Zero" | otherwise = "Negative".
For example 3 ?
isPositive n = if n > 0 then "Positive" else if n == 0 then "Zero" else "Negative".
Step 2 ? Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. In the main function, we bind the value -3 to the variable n, and then call the isPositive function with n as an argument. Finally, we print the result to the console using putStrLn.
Step 3 ? The variable named, "n" is being initialized. It will hold the number which is to be checked whether it is positive or negative.
Step 4 ? The result is printed to the console using ?putStrLn' after the function is called.
Example 1
In this example, we define isPositive function using operators comparison to check whether the number is positive or negative.
isPositive :: Integer -> String isPositive n | n > 0 = "Positive" | n == 0 = "Zero" | otherwise = "Negative" main :: IO () main = do let n = -3 putStrLn (isPositive n)
Output
Negative
Example 2
In this example, a helper function checkSign is used to perform the check. The helper function takes n as an argument and returns the string "Positive" if n is greater than 0, the string "Zero" if n is equal to 0, and the string "Negative" if n is less than 0. The isPositive function calls the helper function and returns the result.
isPositive :: Integer -> String isPositive n = checkSign n where checkSign n | n > 0 = "Positive" | n == 0 = "Zero" | otherwise = "Negative" main :: IO () main = do let n = -3 putStrLn (isPositive n)
Output
Negative
Example 3
In this example, we define isPositive function using if-else statement to check whether the number is positive or negative.
isPositive :: Integer -> String isPositive n = if n > 0 then "Positive" else if n == 0 then "Zero" else "Negative" main :: IO () main = do let n = -3 putStrLn (isPositive n)
Output
Negative
Conclusion
Positive and negative numbers are important concepts in mathematics, as they are used in various mathematical operations, such as addition, subtraction, multiplication, and division. The rules for working with positive and negative numbers help us to perform calculations and understand the relationships between different quantities. In Haskell, we can determine if a number is positive or negative by using operator comparisons or by using if-else statements.