Haskell Program to create a function without argument and without a return value


In Haskell, we can create a function without argument and without a return value by using user-defined functions. In all the examples, we are going to define user-defined functions to perform the certain tasks that don’t return any value and are passed without argument like, printGreeting, printMultipleLines, printSquares 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 without passing any argument.

  • Step 3 − The result is printed to the console, after the function is being called.

Example 1

In this example, printGreeting is a function that simply prints the string "Hello, there!" to the console.

printGreeting :: IO ()
printGreeting = putStrLn "Hello, there!"
main = do
printGreeting

Output

Hello, there!

Example 2

In this example, printMessage uses the do notation to execute two separate putStrLn actions, which print the strings "This is a message." and "Goodbye!" to the console.

printMessage :: IO ()
printMessage = do
   putStrLn "This is a message."
   putStrLn "Goodbye!"
main = do
printMessage

Output

This is a message.
Goodbye!

Example 3

In this example, printMultipleLines uses the mapM_ function to apply the putStrLn function to a list of strings, effectively printing each string on a separate line. The _ in mapM_ indicates that the function is being used for its side-effects (printing to the console), and the result of each action is discarded.

printMultipleLines :: IO ()
printMultipleLines = mapM_ putStrLn ["Line 1", "Line 2", "Line 3"]
main = do
printMultipleLines

Output

Line 1
Line 2
Line 3

Example 4

In this example, printNumbers uses the mapM_ function to print the numbers from 1 to 5.

printNumbers :: IO ()
printNumbers = mapM_ print [1..5]
main = do
printNumbers

Output

1
2
3
4
5

Example 5

In this example, printSquares uses the mapM_ function to print the squares of the numbers from 1 to 5. The (^2) function raises a number to the power of 2, and the (print . (^2)) expression composes the two functions to apply them in sequence.

printSquares :: IO ()
printSquares = mapM_ (print . (^2)) [1..5]
main = do
printSquares

Output

1
4
9
16
25

Example 6

In this example, printSum uses the print function to print the sum of the numbers from 1 to 100. The $ operator is used to apply the print function to the result of the sum function, which adds up the elements of the list..

printSum :: IO ()
printSum = print $ sum [1..100]
main = do
printSum

Output

5050

Example 7

In this example, printCountdown uses the mapM_ function to print the numbers from 10 down to 1. The (putStrLn . show) expression composes the show function (which converts a value to a string) with the putStrLn function, effectively printing the numbers as strings.

printCountdown :: IO ()
printCountdown = mapM_ (putStrLn . show) [10, 9..1]
main = do
printCountdown

Output

10
9
8
7
6
5
4
3
2
1

Example 8

In this example, printFibonacci uses the mapM_ function to print the first 10 Fibonacci numbers. The fib function calculates the nth Fibonacci number, and the mapM_ function applies print . fib to the list of numbers from 1 to 10. The where clause defines the fib function, which uses a simple recursive definition to calculate each number in the sequence.

printFibonacci :: IO ()
printFibonacci = mapM_ (print . fib) [1..10]
   where
      fib n = if n <= 1 then n else fib (n-1) + fib (n-2)
main = do
printFibonacci

Output

1
1
2
3
5
8
13
21
34
55

Example 9

In this example, printFactorial uses the mapM_ function to print the factorials of the numbers from 1 to 5. The factorial function calculates the factorial of a given number, and the mapM_ function applies print . factorial to the list of numbers. The factorial function is defined in the where clause.

printFactorial :: IO ()
printFactorial = mapM_ (print . factorial) [1..5]
   where
      factorial n = if n <= 1 then 1 else n * factorial (n-1)
main = do
printFactorial

Output

1
2
6
24
120

Conclusion

In Haskell, 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 not passing any argument and not returning any value in the function definition.

Updated on: 01-Mar-2023

196 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements