4 ms·
Is that similar to this Haskell code? max :: Ord a => a -> a -> a max a b = if a > b then a else b The concepts that Stepanov was talking about, where
by sketerpot 17y ago
Is that similar to this Haskell code?
max :: Ord a => a -> a -> a
max a b = if a > b then a else b
The concepts that Stepanov was talking about, where algorithms are defined for classes of data structures supporting certain operations (iteration, comparison, etc.), sounds very familiar to anyone who's familiar with Haskell's type classes.
- tumult 17y agoYep
- jemfinch 17y agoThat Haskell code, as I understand it, will dynamically dispatch the call to max through the Ord typeclass dictionary passed at runtime. The C++ code will statically choose the appropriate function.
- klipt 17y agoI'm pretty sure that Haskell typeclasses are also resolved at compile time. That's why you can't have a list containing different ordered types, for example (unless they can all be coerced to the same type).
- jemfinch 17y agoNo, you can't have a list containing different ordered types because the signature for comparison only permits two values of the same exact type, not any arbitrary member of class `Ord`. As I said before, Haskell passes [runtime dictionaries](http://www.haskell.org/haskellwiki/OOP_vs_type_classes http://www.haskell.org/haskellwiki/OOP_vs_type_classes). The compiler can perhaps infer many cases statically, but the core implementation is to pass around runtime dictionaries for typeclasses.