In Haskell, why isn't there a TypeClass for things that can act like lists?

后端 未结 6 957
刺人心
刺人心 2020-12-04 17:59

I\'m reading Learn You a Haskell and I\'m wondering why so many things are acting like a list, and nothing in the Prelude is using the native facility of type classes to set

6条回答
  •  情歌与酒
    2020-12-04 18:18

    There isn't a lot of value to having a type class for list-like data in Haskell. Why? Because of laziness. You can just write a function that converts your data to a list, and then use that list. The list will only get constructed as its sublists and elements are demanded, and their memory will be eligible for collection as soon as no references remain to the prefixes.

    There is value for a type class that provides a generic toList function—however, that already exists in Data.Foldable.

    So basically, the solution is to implement Data.Foldable and use its toList function.

提交回复
热议问题