user3268206
user3268206

Reputation: 21

Haskell Define a numeratic type with bounds

I'm learning Haskell and want to see explore the best practic coding functions with probabilitics distributions.

When coding with probablistic functions, it is typical that a function should return a Float value witin in [0, 1].

Is it possible to define a "Probability" data type that can only take values within that range ?

Thanks very much!

Tao

Upvotes: 2

Views: 64

Answers (1)

Derlin
Derlin

Reputation: 9891

What you ask is not simple. Personally, I would rather use a function like this one:

checkBounds :: (Real a, Show a) => a -> a 
checkBounds x | 0 <= x && x <= 1 = x
          | otherwise = error $ show x ++ " is not in [0,1]"

But if you want to go further, have a look at this article, the probability package and this code snippet, maybe they could help.

Upvotes: 1

Related Questions