Haskell Program to pass an array to the function


In Haskell, we will pass an array to the function by using user-defined functions. In all the examples, we are going to pass an array to the user-defined functions to perform the certain tasks. These functions can be sumArray, maxArray, minArray, countEvens, etc.

In this method, the user-defined functions are created that will contain the function definition with some returning value and is being called by passing an array 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 an array as argument to it.

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

Example 1

In this example, the sumArray function takes an array of Int values as an argument and returns the sum of all elements in the array. The main function demonstrates the usage of the sumArray function by passing an array [1, 2, 3, 4, 5] and printing the result.

sumArray :: [Int] -> Int
sumArray xs = sum xs

main :: IO ()
main = do
   putStrLn "Sum of all elements in an array:"
   let xs = [1, 2, 3, 4, 5]
   let arrSum = sumArray xs
   putStrLn (show xs ++ " has a sum of " ++ show arrSum)

Output

Sum of all elements in an array:
[1,2,3,4,5] has a sum of 15

Example 2

In this example, the maxArray function takes an array of integers xs as an argument and returns the maximum element using the maximum function. The main function creates an array xs, calculates the maximum element using the maxArray function and prints the result.

maxArray :: [Int] -> Int
maxArray xs = maximum xs

main :: IO ()
main = do
   putStrLn "Maximum element in an array:"
   let xs = [1, 2, 3, 4, 5]
   let arrMax = maxArray xs
   putStrLn (show xs ++ " has a maximum element of " ++ show arrMax)

Output

Maximum element in an array:
[1,2,3,4,5] has a maximum element of 5

Example 3

In this example, the minArray function takes an array of integers xs as an argument and returns the minimum element using the minimum function. The main function creates an array xs, calculates the minimum element using the minArray function and prints the result.

minArray :: [Int] -> Int
minArray xs = minimum xs

main :: IO ()
main = do
   putStrLn "Minimum element in an array:"
   let xs = [1, 2, 3, 4, 5]
   let arrMin = minArray xs
   putStrLn (show xs ++ " has a minimum element of " ++ show arrMin)

Output

Minimum element in an array:
[1,2,3,4,5] has a minimum element of 1

Example 4

In this example, the countEvens function takes an array of integers xs as an argument and returns the number of even elements in the array by filtering the even elements with the filter function and returning the length of the resulting list. The main function creates an array xs, calculates the number of even elements using the countEvens function and prints the result.

countEvens :: [Int] -> Int
countEvens xs = length (filter even xs)

main :: IO ()
main = do
   putStrLn "Number of even elements in an array:"
   let xs = [1, 2, 3, 4, 5]
   let count = countEvens xs
   putStrLn (show xs ++ " has " ++ show count ++ " even elements")

Output

Number of even elements in an array:
[1,2,3,4,5] has 2 even elements

Example 5

In this example, the reverseArray function takes an array of integers xs as an argument and returns the reversed array using the reverse function. The main function creates an array xs, calculates the reversed array using the reverseArray function and prints the result.

reverseArray :: [Int] -> [Int]
reverseArray xs = reverse xs

main :: IO ()
main = do
   putStrLn "Reversed array:"
   let xs = [1, 2, 3, 4, 5]
   let reversed = reverseArray xs
   putStrLn (show xs ++ " reversed is " ++ show reversed)

Output

Reversed array:
[1,2,3,4,5] reversed is [5,4,3,2,1]

Conclusion

In Haskell, an array is a collection of values of the same type, stored in contiguous memory locations. An array can be thought of as a sequence of values, where each value is associated with an index. Arrays in Haskell are represented using the [a] type, where a is the type of elements in the array.

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.

Updated on: 01-Mar-2023

442 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements