0

Thomas user said in the comment:

I think modelling the world in terms of "or" (aka discriminated union) and "and" (aka records) is a very powerful functional trick. Sometimes, functions/interfaces are useful too, but if I tend to stick to "or"/"and" as much as I can :-)

Suppose I'm modelling the world of Animal (open-world assumption), how could I do it by using discriminated unions, records and sometimes functions/interfaces?

For example, with animal types, can I use discriminated union to classify them?

type Type =
    | Phyla of Phyla
    | Chordata of Chordata
    | Arthropods of Arthropods
    | ...

and Chordata =
    | Fish
    | Reptiles
    | Mammals
    | ...
Community
  • 1
  • 1
MiP
  • 4,336
  • 2
  • 19
  • 36
  • 3
    This depends on what you're going to _do_ with them. Spherical model in a vacuum makes little sense. – Fyodor Soikin May 04 '17 at 03:24
  • @FyodorSoikin Assume I want to make an animal kingdom simulator. – MiP May 04 '17 at 03:36
  • 2
    One minor point before I go on to a major point: your example code is top-down, but it should really be written bottom-up. I.e., `Chordata` should not need to refer to `Type` in any way. So you should write the definition of `Chordata` first, then the definition of `Type`, and that lets you avoid having any `and` keywords. The presence of `and` in your data model definition is usually a code smell: it is **sometimes** necessary, but it often means that you don't yet have a good representation of the domain. – rmunn May 04 '17 at 04:23
  • 5
    Now for my major point: the best way to learn domain modeling is to do it. You're asking us, basically, to teach you to do good domain modeling, but it's not something that can easily be *explained*. It can only be *shown*. Your best bet is probably to go read some code. Instead of asking how Tomas Petricek *would* model a domain, why not read [how he *actually did* model a domain](https://github.com/tpetricek/FSharp.Formatting/blob/master/src/FSharp.Markdown/Markdown.fs), specifically, parsing Markdown files? That's probably going to teach you more than asking a dozen SO questions. – rmunn May 04 '17 at 04:28
  • F# for Fun and Profit also has some very good guides to domain modelling in F#: https://fsharpforfunandprofit.com/ddd/ https://fsharpforfunandprofit.com/ettt/ – Joe Clay May 04 '17 at 10:49
  • With records or tuples you can only expand your model by adding information to it. As a result you have to start with the most abstract data all your entities have in common. Adding information to this model will inevitably lead to a hierarchy of entities. Discriminated unions on the other hand let you create groups of similar entities. They are far more flexible, because they acknowledge that most data structures involve alternatives and hierarchies are not always a suitable way to represent them efficiently. –  May 04 '17 at 10:53

1 Answers1

2

Functional and object-oriented programming differ in their emphasis of words. "Function" implies a focus on verbs, while "object" implies a focus on nouns.

You may be struggling due to the absence of verbs in the problem description. You mentioned an animal kingdom simulator in the comments; what kind of things happen in that world? I assume the animals don't sit around motionless :-)

Bryan Watts
  • 42,403
  • 15
  • 78
  • 85