In Idris, we define a Rect record which is similar to a struct. Instead of methods, we define functions that take a Rect as an argument.
The area function calculates the area of a rectangle:
And the perim function calculates its perimeter:
In the main function, we create a Rect instance and call our functions:
Idris is a purely functional language, so we don’t need to worry about pointer vs value semantics. There’s no distinction between calling methods on a value or a pointer.
To run this program, you would save it as a .idr file and use the Idris compiler:
In Idris, we use records and functions instead of structs and methods, but the core concept of bundling data with operations on that data remains the same.