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.

Updated on: 19-Jan-2023

383 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements