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

后端 未结 6 963
刺人心
刺人心 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:19

    ByteString is not a generic type.

    In other languages, there is something like Sequence for all list-like data structures. I think this works, with correct extensions:

    class Seq a b | a -> b where
      head :: a -> b
      isTail :: a -> Bool
    
    # ([a]) is a sequence of a's
    instance Seq [a] a where
      head (x:xs) = x
      isTail = (== [])
    
    # ByteString is a sequence of chars
    instance Seq ByteString Char
    

    Or try this?

    type BS a = ByteString
    instance List BS
    

提交回复
热议问题