0
votes

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
    | ...
1
This depends on what you're going to do with them. Spherical model in a vacuum makes little sense.Fyodor Soikin
@FyodorSoikin Assume I want to make an animal kingdom simulator.MiP
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
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, specifically, parsing Markdown files? That's probably going to teach you more than asking a dozen SO questions.rmunn
F# for Fun and Profit also has some very good guides to domain modelling in F#: fsharpforfunandprofit.com/ddd fsharpforfunandprofit.com/etttJoe Clay

1 Answers

2
votes

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 :-)