
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 If an Array Contains a Given Value in Haskell
In Haskell, we will Check if An Array Contains a Given Value by using recursion and foldr & elem functions. In the first example, we are going to use base and recursive cases and in the second example, we are going to use (containsValue x = foldr (\y acc -> acc || x == y) False) function. And in the third example, we are going to use (containsValue val arr = elem val arr) function.
Algorithm
Step 1 ? The recursive containsValue function is defined as,
For example 1 ?
containsValue _ [] = False containsValue x (y:ys) | x == y = True | otherwise = containsValue x ys.
For example 2 ?
containsValue x = foldr (\y acc -> acc || x == y) False.
For example 3 ?
containsValue val arr = elem val arr.
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, a sample array arr and a sample value val are defined and passed to containsValue. The result of the function is then printed.
Step 3 ? The variable named, "arr" is being initialized. It will hold the whole array in which value is to be searched. And the variable named, "val" contains the value that is to be searched in the whole array.
Step 4 ? The result is printed to the console using ?print' function after the function is called.
Example 1
In this example, the recursion is used, checking if the first element of the list is equal to x. If it is, then True is returned. If it's not, then the function is called recursively with the rest of the list (ys) and x. The base case is when the list is empty, in which case False is returned.
containsValue :: (Eq a) => a -> [a] -> Bool containsValue _ [] = False containsValue x (y:ys) | x == y = True | otherwise = containsValue x ys main :: IO () main = do let arr = [1, 2, 3, 4, 5] let val = 3 print (containsValue val arr)
Output
True
Example 2
In this example, a higher-order function foldr is used to check if a value is contained in a list. The function takes a binary function (\y acc -> acc || x == y), an initial accumulator value (False), and the list to fold over (arr). The binary function checks if the current element y is equal to the target value x. If it is, then True is returned. If not, then the current accumulator value is returned.
containsValue :: (Eq a) => a -> [a] -> Bool containsValue x = foldr (\y acc -> acc || x == y) False main :: IO () main = do let arr = [1, 2, 3, 4, 5] let val = 3 print (containsValue val arr)
Output
True
Example 3
In this example, the elem function returns True if the given value is found in the list, and False otherwise. This approach is simpler and more concise than the others.
containsValue :: (Eq a) => a -> [a] -> Bool containsValue val arr = elem val arr main :: IO () main = do let arr = [1, 2, 3, 4, 5] let val = 3 print (containsValue val arr)
Output
True
Conclusion
In Haskell, to check if any array contains the given value or not, we can use recursion, or foldr and elem functions.