Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modelling dice rolls in F#

Tags:

f#

I am doing some Monte Carlo simulations on dice rolls using F# and I am using integers to represent a single roll of a dice and a list of integers to represent a list of dice rolls. I would like to be able to model the dice roll instead of integers as a type. I am only using 6 sided dice and I would like a type that has some of the characteristics of a Alias / Synonym type and some of the properties of either a Discriminated Union or a Enum.

These are what I would like to see on my type

  1. I would like the type to behave like an integer so I can max / min / sum / fold on the lists.
  2. I would like the type to be able to be assigned as an integer value.
  3. I would like the type to be constrained to 1-6 so that there can be no dice roll of a zero and no dice roll of a seven.

I have tried the combination of types listed above and all seem to have some draw backs (which might be my usage and understanding rather than the Types themselves).

This is just a trivial example of something I am doing for fun (not profit) but would like to see an answer as I could imagine using this in more serious data modelling.

like image 202
AlexC Avatar asked Mar 18 '23 23:03

AlexC


1 Answers

As one of the options you could create your own numeric type as described in this post by Tomas Petricek: http://tomasp.net/blog/fsharp-custom-numeric.aspx/index.html

like image 125
Petr Avatar answered Mar 29 '23 10:03

Petr