Haskell Program to calculate the area of Cube


This tutorial will help us in calculating the area of a cube. There are various approaches to calculating the area but the mathematical formula to calculate the area will remain the same I.e., 6*(side^2).

Method 1: Using cubeArea Function

This example defines a function cubeArea that takes a single argument, the length of the cube's sides, and returns the area of the cube. The main function calls the cubeArea function to calculate the area. The result is then printed to the console.

Algorithm

  • Step 1 − The function cubeArea is being defined on the basis of simple mathematical formula i.e., 6a^2 as, cubeArea side = 6 * (side ^ 2).

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

  • Step 3 − A variable named, “side” is being initialized. It will contain the length of the side of the cube.

  • Step 4 − A variable “area” is initialized to hold the computed area value of the cube and the final resultant area value is displayed by using ‘putStrLn’ statement.

Example

In this example, we are going to calculate the area of Cube by using cubeArea function.

module Main where
cubeArea :: Double -> Double
cubeArea side = 6 * (side ^ 2)
main :: IO ()
main = do
   let side = 4
   let area = cubeArea side
   putStrLn ("The area of the cube is: " ++ show area)

Output

The area of the cube is: 96.0

Method 2: Using Data.Fixed

In This method , the Data.Fixed library is used to represent the side length of the cube with a fixed precision. The Fixed E2 type is used to represent a fixed-point decimal with 2 decimal places.

The cubeArea function takes a single argument of type Fixed E2, which represents the length of the cube's sides. It then calculates the area of the cube by multiplying the side length by 6 and raising it to the power of 2.

Algorithm

  • Step 1 − Data.Fixed module is imported.

  • Step 2 − The function cubeArea is being defined by taking a single argument of type Fixed E2, which represents the length of the cube's sides. It then calculates the area of the cube by multiplying the side length by 6 and raising it to the power of 2.

  • Step 3 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.

  • Step 4 − A variable named, “side” is being initialized. It will contain the length of the side of the cube.

  • Step 5 − A variable “area” is initialized to hold the computed area value of the cube once the function is called and the final resultant area value is displayed by using ‘putStrLn’ statement.

Example

In this example, we are going to calculate the area of Cube by using Data.Fixed to represent the side length with a fixed precision.

import Data.Fixed
cubeArea :: Fixed E2 -> Fixed E2
cubeArea side = 6 * (side ^ 2)
main :: IO ()
main = do
   let side = 4
   let area = cubeArea side
   putStrLn ("The area of the cube is: " ++ show area)

Output

The area of the cube is: 96.00

Method 3: Using Pattern Matching

This method defines a data type Cube using record syntax, which has a single field side with a type of Double.

The cubeArea function takes a single argument of type Cube and uses pattern matching to extract the value of the side field. It then calculates the area of the cube by multiplying the side length by 6 and raising it to the power of 2.

Algorithm

  • Step 1 − A data type Cube using record syntax is defined.

  • Step 2 − The cubeArea function is defined by taking a single argument of type Cube and uses pattern matching to extract the value of the side field. It then calculates the area of the cube by multiplying the side length by 6 and raising it to the power of 2.

  • Step 3 − Program execution will be started from main function. The main() function has whole control of the program.

  • Step 4 − A variable named, “side” is being initialized. It will contain the length of the side of the cube.

  • Step 5 − A variable “area” is initialized to hold the computed area value of the cube once the function is called and the final resultant area value is displayed by using ‘putStrLn’ statement.

Example

In the following example, we are going to calculate the area of Cube by using pattern matching.

data Cube = Cube { side :: Double }
cubeArea :: Cube -> Double
cubeArea (Cube side) = 6 * (side ^ 2)
main :: IO ()
main = do
   let side = 4
   let area = cubeArea (Cube side)
   putStrLn ("The area of the cube is: " ++ show area)

Output

The area of the cube is: 96.0

Conclusion

There are different ways to calculate the area of a cube. In Haskell, the area of a cube can be calculated by using cubeArea function, by using Data.Fixed library or by using pattern matching.

Updated on: 19-Jan-2023

109 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements