Haskell Program to calculate the volume, diagonal, and area of Cuboids


This tutorial will help us in calculating the volume, diagonal, and area of the Cuboids. The volume of a cuboid is a measure of the amount of space inside the cuboid. And area involves the surface area of the cuboid. The diagonal of a cuboid is a line segment connecting two opposite vertices of the cuboid. It is also known as the "space diagonal" or "body diagonal" of the cuboid.

Algorithm

  • Step 1 − The volume, diagonal and area functions are defined on the basis of simple mathematical formula as volume l w h = l * w * h ; diagonal l w h = sqrt (l^2 + w^2 + h^2) and area l w h = 2 * (l*w + w*h + h*l) respectively.

  • 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. The main function takes input from the user for the length, width, and height of the cuboid and then uses the above functions to calculate the volume, diagonal, and surface area.

  • Step 3 − The variables named, “length” , “width” and “height” is being initialized. It will contain the length, width and height of the cuboid whose area, diagonal and volume is to be computed.

  • Step 4 − The final resultant volume, diagonal and area value is displayed once the volume, diagonal and area functions are called.

Using User-defined Functions

This example defines three functions: volume, diagonal, and area, which respectively calculate the volume, diagonal, and surface area of a cuboid. Finally, it displays the results to the user.

Example 

volume :: Float -> Float -> Float -> Float
volume l w h = l * w * h

diagonal :: Float -> Float -> Float -> Float
diagonal l w h = sqrt (l^2 + w^2 + h^2)

area :: Float -> Float -> Float -> Float
area l w h = 2 * (l*w + w*h + h*l)

main :: IO ()
main = do
   let l = 5
   let w = 4
   let h = 10
   let v = volume l w h
       d = diagonal l w h
       a = area l w h
   putStrLn ("Volume: " ++ show v)
   putStrLn ("Diagonal: " ++ show d)
   putStrLn ("Surface Area: " ++ show a)

Output

Volume: 200.0
Diagonal: 11.874342
Surface Area: 220.0

Using a Data Type

This example defines a custom data type Cuboid that contains the cuboid’s length, width, and height. The volume, diagonal, and area functions take a Cuboid as input and return the calculated values. Finally, it displays the results to the user.

Example 

data Cuboid = Cuboid { length :: Float, width :: Float, height :: Float }

volume :: Cuboid -> Float
volume (Cuboid l w h) = l * w * h

diagonal :: Cuboid -> Float
diagonal (Cuboid l w h) = sqrt (l^2 + w^2 + h^2)

area :: Cuboid -> Float
area (Cuboid l w h) = 2 * (l*w + w*h + h*l)

main :: IO ()
main = do
    putStrLn "Enter the length, width, and height of the cuboid: "
    let l = 5
    let w = 4
    let h = 10
    let cuboid = Cuboid l w h
        v = volume cuboid
        d = diagonal cuboid
        a = area cuboid
    putStrLn ("Volume: " ++ show v)
    putStrLn ("Diagonal: " ++ show d)
    putStrLn ("Surface Area: " ++ show a)

Output

Volume: 200.0
Diagonal: 11.874342
Surface Area: 220.0

Conclusion

There are different ways to calculate the volume, diagonal and area of a cuboid. In Haskell, the volume, diagonal and the area of a cuboid can be calculated by using user-defined function or by using a data type to represent the cuboid and a set of functions to operate on it.

Updated on: 23-Jan-2023

126 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements