Methods in Scheme
Scheme supports procedures defined on custom data types, which we can implement using records.
To run the program, save it in a file (e.g., methods.scm
) and use your Scheme interpreter. For example, if you’re using Guile:
In Scheme, we don’t have the concept of methods attached to objects like in object-oriented languages. Instead, we define procedures that operate on our custom data types (records in this case). The area
and perim
procedures take a rect
as an argument and perform calculations based on its properties.
Scheme doesn’t have a built-in distinction between value and reference semantics like some other languages. The behavior depends on how the Scheme implementation handles records. In many implementations, records are handled by reference, so there’s no need for explicit pointer manipulation.
Next, we’ll look at Scheme’s mechanism for creating abstract interfaces: generic procedures and method dispatch.