- 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 check whether a variable is defined or not
This tutorial will help us in checking whether a variable is defined or not. In Haskell, a variable is considered defined if it has a value assigned to it. The value can be of any type, including a special type called Maybe that is used to represent values that may or may not be present.
Algorithm
Step 1 − The Data.Maybe module is imported.
Step 2 − The checkDefined function is defined as, checkDefined x = isJust x. It takes a Maybe value and returns a Bool indicating whether the value is Just (i.e. defined) or Nothing (i.e. not defined).
Step 3 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.
Step 4 − A variable named, “x” is being initialized. It will contain the resultant bool value for the value passed that is to be checked, whether it is defined or not once the function is called.
Step 5 − If the variable is defined, “True” is printed otherwise if variable is not defined, “False” is printed.
Example 1: Using checkDefined Function
In this example, the checkDefined function takes a Maybe value and returns a Bool indicating whether the value is Just (i.e. defined) or Nothing (i.e. not defined). The main function demonstrates how the function can be used by defining two variables, x and y, and checking whether they are defined using the checkDefined function.
import Data.Maybe checkDefined :: Maybe a -> Bool checkDefined x = isJust x main :: IO () main = do let x = Just 5 print (checkDefined x) let y = Nothing print (checkDefined y)
Output
True False
Example 2: By Using isNothing Function
In this example, the checkDefined function uses the isNothing function from the Data.Maybe module to check if the input Maybe value is Nothing. If it is, the function returns False, otherwise it returns True.
import Data.Maybe checkDefined :: Maybe a -> Bool checkDefined x = not (isNothing x) main :: IO () main = do let x = Just 5 print (checkDefined x) let y = Nothing print (checkDefined y)
Output
True False
Example 3: Using mayToList Function
In This example, the checkDefined function uses the maybeToList function to convert the input Maybe value to a list, and then checks the length of the list. If the length is greater than 0, the function returns True, indicating that the value is defined, otherwise it returns False, indicating that the value is not defined.
import Data.Maybe checkDefined :: Maybe a -> Bool checkDefined x = length (maybeToList x) > 0 main :: IO () main = do let x = Just 5 print (checkDefined x) let y = Nothing print (checkDefined y)
Output
True False
Conclusion
In Haskell, to check if a variable is defined, we can use the various functions provided by the Data.Maybe module, such as isJust, isNothing, maybe, fromMaybe, maybeToList etc. These functions take a Maybe a value as input and return a Bool value indicating whether the value is defined or not. We can also use pattern matching to check if a Maybe value is Just or Nothing.
- Related Articles
- Haskell Program to Check Whether a Character is Alphabet or Not
- Haskell Program to Check Whether a Number is Prime or Not
- Haskell Program to Check Whether a Number is Even or Odd
- Haskell Program to Check Whether a Number is Positive or Negative
- How to check a not-defined variable in JavaScript?
- Haskell Program to check a given number is finite or not
- How can I check whether a variable is defined in JavaScript?
- Haskell Program to Check Whether an Alphabet is Vowel or Consonant
- C# program to check whether a list is empty or not
- Python program to check whether a list is empty or not?
- C++ Program to Check Whether a Number is Prime or Not
- C++ Program to Check Whether a Number is Palindrome or Not
- C Program to Check Whether a Number is Prime or not?
- Java Program to Check Whether a Character is Alphabet or Not
- Java Program to Check Whether a Number is Prime or Not
