Explain why the Haskell Prelude function length is polymorphic
时间: 2024-01-24 20:16:51 浏览: 93
The real Haskell
The Haskell Prelude function length is polymorphic because it can operate on lists of any type. The function does not depend on the specific type of the elements in the list, only on the fact that the list is a collection of values that can be counted. Therefore, the length function can accept a list of any type, making it a polymorphic function.
For example, the following are valid uses of the length function in Haskell:
```
length [1, 2, 3] -- returns 3
length ["hello", "world"] -- returns 2
length ['a', 'b', 'c'] -- returns 3
```
In each case, the length function is applied to a list of a different type, but it can still determine the number of elements in the list regardless of the type of those elements.
阅读全文