- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- 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 create a function with arguments but without a return value
In Haskell, we will create a function with arguments but without a return value by using user-defined functions. In this article, the user-defined functions are defined that will contain the function definition without returning any value and is being called by passing some desired arguments to it.
In all the examples, we are going to define user-defined functions to perform the certain tasks that don’t return any value but are passed with arguments like, printSum, printString , printList and other functions.
Step 1 − The user defined function is defined by writing its definition without returning any 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 some desired arguments to it.
Step 3 − The result is printed to the console, after the function is being called.
In this example, the function printSum takes two Int arguments x and y, and uses the putStrLn function from the IO monad to print the sum of the arguments to the console. The return type of printSum is IO (), indicating that it's an IO action that doesn't return a value.
printSum :: Int -> Int -> IO () printSum x y = putStrLn (show x ++ " + " ++ show y ++ " = " ++ show (x + y)) main = do printSum 2 3
2 + 3 = 5
In this example, printString takes a String argument and uses putStrLn to print it to the console.
printString :: String -> IO () printString str = putStrLn str main = do printString "hello"
In this example, printList takes a list as an argument and print it to the console.
printList :: [Int] -> IO () printList xs = mapM_ print xs main = do printList [1..5]
1 2 3 4 5
In this example, concatenate takes two Strings as arguments, concatenate them and uses putStrLn to print it to the console.
concatenate :: String -> String -> IO () concatenate str1 str2 = putStrLn (str1 ++ str2) main = do concatenate "hello" "India"
In this example, printProduct takes two integers as arguments, multiply them and uses putStrLn to print their product to the console.
printProduct :: Int -> Int -> IO () printProduct x y = putStrLn (show x ++ " * " ++ show y ++ " = " ++ show (x * y)) main = do printProduct 3 4
3 * 4 = 12
In this example, printMax takes a list of integers as arguments, compare them and uses putStrLn to print the max integer to the console.
printMax :: [Int] -> IO () printMax xs = putStrLn (show (maximum xs)) main = do printMax [1,7,9,5,4]
In this example, printStringsTogether takes two strings as arguments, and uses putStrLn to print them side-by-side to the console.
printStringsTogether :: String -> String -> IO () printStringsTogether str1 str2 = putStrLn (str1 ++ " " ++ str2) main = do printStringsTogether "Hello" "World"
In this example, printOddNumbers takes a list of integers as arguments, compare them and uses putStrLn to print the odd numbers to the console.
printOddNumbers :: [Int] -> IO () printOddNumbers xs = mapM_ print (filter odd xs) main = do printOddNumbers [1,5,3,2,4,6,7,8]
1 5 3 7
In this example, printStringWithBorder takes a string as argument, and uses putStrLn to print it with borders to the console.
printStringWithBorder :: String -> IO () printStringWithBorder str = do putStrLn (replicate (length str + 4) '*') putStrLn ("* " ++ str ++ " *") putStrLn (replicate (length str + 4) '*') main = do printStringWithBorder "Hello"
********* * Hello * *********
In this example, printFirstCharacters takes a list of strings as argument, and uses putStrLn to print the first character of every string in the list to the console.
printFirstCharacters :: [String] -> IO () printFirstCharacters strings = mapM_ (\str -> putStrLn (take 1 str)) strings main = do printFirstCharacters ["Hello","India"]
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 even by passing arguments and not returning any value in the function definition.
In Haskell, functions without a return value and with arguments are typically referred to as "action functions." They are functions that perform some action (such as printing to the screen, reading from the keyboard, etc.) but don't return a value. These functions are defined in the IO monad, and their return type is typically IO ().
Kickstart Your Career
Get certified by completing the courseGet Started