使用Poly / ML,我想编写一个函数来使用Array of Arrays构造一个n * n单位矩阵。
我写道:
fun equiv x y = if x = y then 1 else 0;
fun idmatrix n = Array.tabulate(n, fn i => (Array.tabulate(n, equiv i)));
并且编译成功,给出了
> val equiv = fn: ''a -> ''a -> int
> val idmatrix = fn: int -> int array array
但是当我打电话给 idmatrix
时idmatrix 2;
对于测试,编译器的输出是
> Exception- Cast "toAddress" raised
有人可以解释为什么要提出例外吗?
非常感谢!
答案 0 :(得分:2)
您的代码适用于我。看the code that raises the exception,也许是32/64位地址问题?您的Poly / ML是否针对正确的架构进行了编译?
多晶硅/ ML:
$ poly
Poly/ML 5.2 Release
> fun equiv x y = if x = y then 1 else 0;
val equiv = fn : ''a -> ''a -> int
> fun idmatrix n = Array.tabulate(n, fn i => (Array.tabulate(n, equiv i)));
val idmatrix = fn : int -> int Array.array Array.array
> idmatrix 3;
val it = fromList[fromList[1, 0, 0], fromList[0, 1, 0], fromList[0, 0, 1]]
: int Array.array Array.array
莫斯科ML:
$ mosml
Moscow ML version 2.10
Enter `quit();' to quit.
- fun equiv x y = if x = y then 1 else 0;
> val ''a equiv = fn : ''a -> ''a -> int
- fun idmatrix n = Array.tabulate(n, fn i => (Array.tabulate(n, equiv i)));
> val idmatrix = fn : int -> int array array
- idmatrix 3;
> val it = <array> : int array array
和SML / NJ:
$ sml
Standard ML of New Jersey v110.76 [built: Sun Jun 29 03:29:51 2014]
- fun equiv x y = if x = y then 1 else 0;
stdIn:1.23 Warning: calling polyEqual
val equiv = fn : ''a -> ''a -> int
- fun idmatrix n = Array.tabulate(n, fn i => (Array.tabulate(n, equiv i)));
[autoloading]
[library $SMLNJ-BASIS/basis.cm is stable]
[autoloading done]
val idmatrix = fn : int -> int array array
- idmatrix 3;
val it = [|[|1,0,0|],[|0,1,0|],[|0,0,1|]|] : int array array