F#: how to find Cartesian power
Posted
by Nike
on Stack Overflow
See other posts from Stack Overflow
or by Nike
Published on 2010-04-15T19:54:49Z
Indexed on
2010/04/15
20:53 UTC
Read the original article
Hit count: 314
F#
|functional-programming
I have a problem with writing a Cartesian power function. I found many examples about calculating Cartesian Product, but no one about Cartesian power.
For example, [1;2] raised to power 3 = [ [1;1;1] ; [1;1;2] ; [1;2;1] ; [1;2;2] ; [2;1;1] ; [2;1;2] ; [2;2;1]; [2;2;2] ]
I use following code to calculate Cartesian Product:
let Cprod U V =
let mutable res = []
for u in U do
for v in V do
res <- res @ [[u;v]]
res
And trying to calculate Cartesian power. I use following code to calculate Cartesian Product:
let Cpower U n =
let mutable V = U
for i=0 to n-1 do
V <- Dprod U V
V
Visual Studio said: Error The resulting type would be infinite when unifying ''a' and ''a list'. I will thankful for any help and links.
© Stack Overflow or respective owner