
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 Pass a String to the Function
This article will help us learn how to pass a string to the function in Haskell with identity function and lambda expression. In the first example, we are going to use (myFunction inputString = inputString) function and in the second example, we are going to use (myFunction = id). And in third example, we are going to use lambda expression, (myFunction = \inputString -> inputString).
Method 1: Passing a string to the user-defined function
In this method, the user-defined functions are defined that will contain the function definition with some returning value and is being called by passing a string as argument to it.
Algorithm
Step 1 ? The user defined function is defined by writing its definition with a return value.
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 by passing a string as argument to it.
Step 3 ? The result is printed to the console, after the function is being called.
Example
In this example, the function myFunction is defined which takes a String as input and returns it as output. The main function then calls the myFunction with the argument "Hello, World!" and assigns the result to the variable result. Finally, the putStrLn function is used to print the value of result, which is "Hello, World!", to the console.
myFunction :: String -> String myFunction inputString = inputString main = do let result = myFunction "Hello, World!" putStrLn result
Output
Hello, World!
Method 2: Passing a string to the user-defined function using identity function
In this method, the user-defined functions are defined using identity function to hold the string that will contain the function definition with some returning value and is being called by passing a string as argument to it.
Algorithm
Step 1 ? The user defined function is defined using identity function by writing its definition with a return value.
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 by passing a string as argument to it.
Step 3 ? The result is printed to the console, after the function is being called.
Example
In this example, a function "myFunction" is defined that takes a string as input and returns the same string as output. The "id" function is the identity function which returns its argument unchanged. The "main" function then uses "myFunction" by passing the string "Using Identity function!" as an argument and binds the result to the "result" variable. The "putStrLn" function is then used to print the value of "result" to the console.
myFunction :: String -> String myFunction = id main = do let result = myFunction "Using Identity function!" putStrLn result
Output
Using Identity function!
Method 3: Passing a string to the user-defined function using Lambda Expression
In this method, the user-defined functions are defined using lambda expression that will contain the function definition with some returning value and is being called by passing a string as argument to it.
Algorithm
Step 1 ? The user defined function is defined using lambda expression by writing its definition with a return value.
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 by passing a string as argument to it.
Step 3 ? The result is printed to the console, after the function is being called.
Example
In this example, a Haskell function myFunction is defined using a lambda expression. The lambda expression \inputString -> inputString takes an argument inputString and returns it unchanged. The main function then calls the myFunction with the argument "Using Lambda Expression!" and assigns the result to the variable result. Finally, the putStrLn function is used to print the value of result, which is "Using Lambda Expression!", to the console.
myFunction :: String -> String myFunction = \inputString -> inputString main = do let result = myFunction "Using Lambda Expression!" putStrLn result
Output
Using Lambda Expression!
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 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 some value in the function definition. The arguments passed can be integers, strings or any array of the values.