4 ms·
Not all ML/Haskell functions are semantically transparent based on the type signature, but type signatures are really useful. You might wonder what function yo
by time_management 18y ago
Not all ML/Haskell functions are semantically transparent based on the type signature, but type signatures are really useful.
You might wonder what function you use to find the length of a list in ML. The function obviously has to exist, but there are a number of things it could be called (length? count? size?). At the toploop, you type "Module L = List" and you get the module's type signature. You see that a function called length exists with type signature 'a list -> int. You know that that's the function you're looking for.
Other functions' type signatures give a great indication of what the functions do. For example,
List.map : ('a -> 'b) -> 'a list -> 'b list
List.filter : ('a -> bool) -> 'a list -> 'a list
List.filter_map : ('a -> 'b option) -> 'a list -> 'b list
All of these do the most intuitive thing that a function with that type signature should do. Obviously, not all functions can indicate their semantics through their type signature. For example, you might have one called partition with the following signature:
partition : ('a -> bool) -> 'a list -> ('a list) * ('a list).
You don't know whether the "true" list appears first or second in the returned tuple, but you can easily check this at the toploop.
When you're reading other peoples' code, static typing can be a huge win. If the writer of the code used the type system properly, this cuts your read-work by 80%, even without any other documentation.