5 ms·
I'm happy with myself for at least mostly getting what those were. I think I'd have to see more uses to really see the benefit, though. I am curious on minBou
by taeric 12y ago
I'm happy with myself for at least mostly getting what those were. I think I'd have to see more uses to really see the benefit, though.
I am curious on minBound and maxBound. They seem to be the same... What distinguishes them? Or, you are just saying these are two values that are defined. And they can only be given a value of a type that can be bounded?
Also, thanks for expanding!
- hornetblack 12y agominBound and maxBound are typeclass functions. Typeclass's are a similar to java interfaces. So the Bounded typeclass is defined as class Bounded a where minBound :: a maxBound :: a If you have a function and you want the argument to be Bounded you write allLessThan :: (Enum a, Bounded a)=> a -> [a] allLessThan x = [minBound..x] Here `a` is a generic type. That implements the Enum and Bounded typeclass. You could just write `allLessThan x = [minBound..x]` and Haskell with infer the type classes by itself. To implement the typeclass you use `instance`, like so: data MyType = A | B | C | D instance Bounded MyType where minBound = A maxBound = D
- dllthomas 12y agoHopefully hornetblack's answer shed some light, but the most important bit is that the most of the above were just type signatures. If they're part of a typeclass you would define them for a particular type in the instance. Otherwise, you'd define them generically using only other functions defined to generically work with the same constraints (or looser). So in the case of minBound and maxBound in particular, you'd define them appropriately for a particular type when you declare a type to be Bounded.