Is there an algorithm to lead all possible combinations of given amount of three-valued logic values?
For example, F(2)
should return this list:
t t
t u
t f
u t
u u
u f
f t
f u
f f
The function would look like this (in Haskell):
data Tril = FALSE | NULL | TRUE
all :: Int -> [[Tril]]
all amount = ???
all1 :: [Tril]
all1 = join (all 1)
all2 :: [(Tril, Tril)]
all2 = map (\[f, s] -> (f, s)) (all 2)
all3 :: [(Tril, Tril, Tril)]
all3 = map (\[f, s, t] -> (f, s, t)) (all 3)
You can do this very simply as a list comprehension:
You can write it equivalently as a monadic do-block:
And that gives us an idea for how we can write the variable-size one:
As it turns out — and this is slightly mind-bending — this is the net effect of the
replicateM
function. It takes a monadic action, does it N times, and gathers the results together.