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.

Algorithm

  • 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.

Example 1

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

Output

2 + 3 = 5

Example 2

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"

Output

hello

Example 3

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]

Output

1
2
3
4
5

Example 4

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"

Output

helloIndia

Example 5

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

Output

3 * 4 = 12

Example 6

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]

Output

9

Example 7

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"

Output

Hello World

Example 8

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]

Output

1
5
3
7

Example 9

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"

Output

*********
* Hello *
*********

Example 10

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"]

Output

H
I

Conclusion

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 ().

Updated on: 01-Mar-2023

174 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements