How to model class hierarchies in Haskell?

前端 未结 4 1819
傲寒
傲寒 2021-02-04 05:33

I am a C# developer. Coming from OO side of the world, I start with thinking in terms of interfaces, classes and type hierarchies. Because of lack of OO in Haskell, sometimes I

4条回答
  •  清酒与你
    2021-02-04 06:12

    Haskell is my favorite language, is a pure functional language. It does not have side effects, there is no assignment. If you find to hard the transition to this language, maybe F# is a better place to start with functional programming. F# is not pure.

    Objects encapsulate states, there is a way to achieve this in Haskell, but this is one of the issues that takes more time to learn because you must learn some category theory concepts to deeply understand monads. There is syntactic sugar that lets you see monads like non destructive assignment, but in my opinion it is better to spend more time understanding the basis of category theory (the notion of category) to get a better understanding.

    Before trying to program in OO style in Haskell, you should ask yourself if you really use the object oriented style in C#, many programmers use OO languages, but their programs are written in the structured style.

    The data declaration allows you to define data structures combining products (equivalent to structure in C language) and unions (equivalent to union in C), the deriving part o the declaration allows to inherit default methods.

    A data type (data structure) belongs to a class if has an implementation of the set of methods in the class. For example, if you can define a show :: a -> String method for your data type, then it belong to the class Show, you can define your data type as an instance of the Show class.

    This is different of the use of class in some OO languages where it is used as a way to define structures + methods.

    A data type is abstract if it is independent of it's implementation. You create, mutate, and destroy the object by an abstract interface, you do not need to know how it is implemented.

    Abstraction is supported in Haskell, it is very easy to declare. For example this code from the Haskell site:

    data Tree a = Nil 
                | Node { left  :: Tree a,
                         value :: a,
                         right :: Tree a }
    

    declares the selectors left, value, right. the constructors may be defined as follows if you want to add them to the export list in the module declaration:

    node = Node 
    nil = Nil
    

    Modules are build in a similar way as in Modula. Here is another example from the same site:

    module Stack (Stack, empty, isEmpty, push, top, pop) where
    
    empty :: Stack a
    isEmpty :: Stack a -> Bool
    push :: a -> Stack a -> Stack a
    top :: Stack a -> a
    pop :: Stack a -> (a,Stack a)
    
    newtype Stack a = StackImpl [a] -- opaque!
    empty = StackImpl []
    isEmpty (StackImpl s) = null s
    push x (StackImpl s) = StackImpl (x:s)
    top (StackImpl s) = head s
    pop (StackImpl (s:ss)) = (s,StackImpl ss)
    

    There is more to say about this subject, I hope this comment helps!

提交回复
热议问题