
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
Haskell Program to Return a String from a Function
In this article, we are going to learn how to return a string from a function using user-defined function along with record syntax and let binding. In the first example, we are going to use (myFunction = "Hello, World!") function and in the second example, we are going to use (myFunction = stringValue myData). And in third example, we are going to use let binding, (myFunction = let str = "Using let binding!" in str).
Method 1: Returning a string from the user-defined function
In this method, the user-defined functions are defined that will contain the function definition and return a string once the function is being called.
Algorithm
Step 1 ? The user defined function is defined by writing its definition that will return a string 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 user defined function is being called.
Step 3 ? The resultant string is printed to the console, after the function is being called.
Example 1
In this example, myFunction is a function that returns the string, "Hello, World!". The main function calls myFunction and prints the result to the console.
myFunction :: String myFunction = "Hello, World!" main = do let result = myFunction putStrLn result
Output
Hello, World!
Example 2
In this example, a Haskell function myFunction is defined using a case expression. The case expression case str of str -> str matches the value of str and returns it. The value of str is defined in the where clause as "Hello, World!".
myFunction :: String myFunction = case str of str -> str where str = "Hello, World!" main = do let result = myFunction putStrLn result
Output
Hello, World!
Method 2: Returning a string from the user-defined function using record syntax
In this method, the user-defined functions are defined using record syntax that will contain the function definition and return a string once the function is being called.
Algorithm
Step 1 ? The user defined function is defined by writing its definition using record syntax that will return a string 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 user defined function is being called.
Step 3 ? The resultant string is printed to the console, after the function is being called.
Example
In this example, a record data type MyData is used with a single field stringValue of type String, which holds the value "Hello, World!". The myFunction then uses pattern matching to extract the stringValue from the MyData value myData and returns it.
data MyData = MyData { stringValue :: String } myFunction :: String myFunction = stringValue myData where myData = MyData { stringValue = "Using Record Syntax!" } main = do let result = myFunction putStrLn result
Output
Using Record Syntax!
Method 3: Returning a string from the user-defined function using let binding
In this method, the user-defined functions are defined using let binding that will contain the function definition and return a string once the function is being called.
Algorithm
Step 1 ? The user defined function is defined by writing its definition using the let binding that will return a string 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 user defined function is being called.
Step 3 ? The resultant string is printed to the console, after the function is being called.
Example
In this example, a Haskell function myFunction is defined using a let binding. The let binding str = "Using let binding!" defines a local variable str with the value "Using let binding!". The myFunction then returns the value of str.
myFunction :: String myFunction = let str = "Using let binding!" in str main = do let result = myFunction putStrLn result
Output
Using let binding!
Conclusion
In Haskell, a string is a sequence of characters, represented as a list of characters. A string can be defined using double quotes (").
The user-defined functions are functions created by the programmer to perform specific operations. The users can define functions as per their need by passing any desired arguments and returning some value in the function definition. The arguments passed can be integers, strings or any array of the values and the corresponding resultant string is returned and printed to console.