
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
Return Multiple Values from a Function in Haskell
In Haskell, we can return multiple values from the function by using user-defined function along with tuple, custom data type and Maybe data type. In the first example, we are going to use (myFunction = ("Hello, World!", 42)) and in the second example, we are going to use (data MyData = MyData { stringValue :: String, intValue :: Int }). And in third example, we are going to use, (myFunction :: Maybe (String, Int)).
Method 1: Using Tuple
In this method, a function can return multiple values by using a tuple. A tuple is a data structure that holds multiple values of different or same types.
Algorithm
Step 1 ? The user defined function is defined by writing its definition holding tuple that will return the multiple values after computation.
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, the myFunction is called and its result is unpacked using pattern matching and assigned to the variables resultString and resultInt.
Step 3 ? The putStrLn function is then used to print the value of resultString to the console, and the print function is used to print the value of resultInt.
Example
In this example, a Haskell function myFunction is defined that returns a tuple containing two values, a string "Hello, World!" and an integer 42.
myFunction :: (String, Int) myFunction = ("Hello, World!", 42) main = do let (resultString, resultInt) = myFunction putStrLn resultString print resultInt
Output
Hello, World! 42
Method 2: Returning multiple values from the user-defined function using a custom data type
In this method, a custom data type MyData is used with two fields, stringValue and intValue, to store the values to be returned. The myFunction returns a value of type MyData constructed using record syntax.
Algorithm
Step 1 ? The user defined function is defined by writing its definition using a custom data type that will return the multiple values after computation.
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, the myFunction is called and its result is assigned to the variable result. The values of the fields stringValue and intValue are then accessed using the dot notation and passed to putStrLn and print functions, respectively, to print them to the console.
Step 3 ? The putStrLn function is then used to print the value of String to the console, and the print function is used to print the value of Integer.
Example
In this example, a Haskell function myFunction is defined that returns two values using custom data type, a string "Hello, World!" and an integer 42.
data MyData = MyData { stringValue :: String, intValue :: Int } myFunction :: MyData myFunction = MyData { stringValue = "Hello, World!", intValue = 42 } main = do let result = myFunction putStrLn (stringValue result) print (intValue result)
Output
Hello, World! 42
Method 3: Returning multiple values from the user-defined function using the Maybe data type
In this method, the Maybe data type is used, which is used to represent a value that might or might not be present. The myFunction returns a value of type Maybe (String, Int), which is a Just value containing a tuple of string and integer values in this case.
Algorithm
Step 1 ? The user defined function is defined by writing its definition using the Maybe data type that will return the multiple values after computation.
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, the myFunction is called and its result is assigned to the variable result. The value of result is then pattern matched using a case expression, and either the string and integer values are printed to the console using the putStrLn and print functions, respectively, or a message "No values found." is printed if the result is Nothing.
Step 3 ? The putStrLn function is then used to print the value of String to the console, and the print function is used to print the value of Integer.
Example
In this example, a Haskell function myFunction is defined that returns two values using the Maybe data type, a string "Hello, World!" and an integer 42.
myFunction :: Maybe (String, Int) myFunction = Just ("Hello, World!", 42) main = do let result = myFunction case result of Just (str, i) -> do putStrLn str print i Nothing -> putStrLn "No values found."
Output
Hello, World! 42
Conclusion
In Haskell, there are several ways to return multiple values from a function, including using tuples, using the either data type, using the Maybe data type or using custom data type. The user-defined functions are functions that are created by the programmer to perform specific operations. The users can define functions as per their need by passing any desired arguments and returning multiple values from the function.