Haskell Program to return an array from the function


This article will help us learn how to return an array from the function in haskell using user-defined function along with list comprehension and recursion. In the first example, we are going to use (show (getArray)) function and in the second example, we are going to use (getArray n = [x | x <- [1..n]]). And in third example, we are going to use recursion with base and recursive case.

Method 1: Returning an array from the user-defined function

In this method, the user-defined functions are defined that will contain the function definition and return an array once the function is being called.

Algorithm

  • Step 1 − The user defined function is defined by writing its definition that will return an array after computation.

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

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

Example

In this example, getArray is a function that returns a list of integers, [1, 2, 3, 4, 5]. The main function calls getArray and prints the result to the console.

getArray :: [Int]
getArray = [1, 2, 3, 4, 5]

main :: IO ()
main = do
   putStrLn (show (getArray))

Output

[1,2,3,4,5]

Method 2: Returning an array from the user-defined function using list comprehension

In this method, the user-defined functions are defined that will contain the function definition and return an array using list comprehension once the function is being called.

Algorithm

  • Step 1 − The user defined function is defined by writing its definition that will return an array after computation. To return the array, the list comprehension is used.

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

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

Example 1

In this example, getArray takes an integer n as input and returns a list of integers [1..n]. The main function calls getArray 5 and prints the result to the console.

getArray :: Int -> [Int]
getArray n = [x | x <- [1..n]]

main :: IO ()
main = do
   putStrLn (show (getArray 5))

Output

[1,2,3,4,5]

Example 2

In this example, getArray takes a function f and a list xs as input and returns a new list that is the result of applying f to each element of xs. The main function calls getArray (*2) [1, 2, 3, 4, 5], which applies the function (*2) to each element of the list [1, 2, 3, 4, 5], and prints the result to the console.

getArray :: (Int -> Int) -> [Int] -> [Int]
getArray f xs = map f xs

main :: IO ()
main = do
   putStrLn (show (getArray (*2) [1, 2, 3, 4, 5]))

Output

[2,4,6,8,10]

Method 3: Returning an array from the user-defined function using recursive function

In this method, the user-defined functions are defined that will contain the function definition and return an array using recursive function once the function is being called.

Algorithm

  • Step 1 − The user defined function is defined by writing its definition that will return an array after computation. To return the array, the recursive function is used.

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

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

Example 

In this example, getArray takes an integer n as input and returns a list of integers [n, n-1, ..., 1]. The function uses recursion to build the list. The base case is when n is 0, in which case the function returns an empty list []. The main function calls getArray 5 and prints the result to the console.

getArray :: Int -> [Int]
getArray 0 = []
getArray n = n : getArray (n - 1)

main :: IO ()
main = do
   putStrLn (show (getArray 5))

Output

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

Updated on: 01-Mar-2023

332 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements