一个函数调用另一个函数
A function calling another function
假设我有两个 SML 函数 Ping 和 Pong.My 代码是这样的:
val arr=Array.array(10,0)
fun Ping (arr,_,[],_)=true
|Ping (arr,0,(x::xs),ping_list)=Ping(arr,1,xs,Pong(arr,ping_list,10)) //<-Problem is here
| Ping (arr,K,(x :: xs),ping_list) =
(if Array.sub(arr,x)=0
then
Array.update(arr, x, Array.sub(arr,x)+1)
else
Array.update(arr, x, Array.sub(arr,x)+1);
if Array.sub(arr,x)-1<1 then Ping (arr,(K-1),xs,(x::ping_list)) else Ping (arr,K,xs,(x::ping_list)))
fun Pong arr [] _ =[]
|Pong arr (x::xs) min=
(if Array.sub(arr,x)>=1
then Array.update(arr,x,Array.sub(arr,x)-1)
else();
if Array.sub(arr,x)+1>1
then if (length xs)<min then Pong arr xs (length xs)
else Pong arr xs min
else xs)
如果我用数组 3:int,[1,2,3]:list,[] 调用 Ping,如果我用array,list,integer 它给我输出 i want.In ping funcrion 如果 K=0 我想调用 pong 函数(修改数组)并递归调用 Ping 函数但是在参数 ping_list 中有Pong 的结果(这是一个列表)但是 运行 这给了我这个错误:
stdIn:27.40-27.73 Error: operator and operand do not agree [tycon mismatch]
operator domain: int array
operand: 'Z * 'Y * [int ty]
in expression:
Pong (arr,ping_list,10)
我也尝试过将 Ping 和 Pong 放在本地并从中调用 Ping,但似乎没有 work.How 这可以解决吗?
Control.Print.printDepth := 20;
val arr = Array.array(10,0)
fun Ping (arr, _, [], _) = true
|Ping (arr, 0, (x::xs), ping_list) = Ping(arr, 1, xs, Pong(arr, ping_list, 10))
|Ping (arr, K, (x :: xs), ping_list) =
(if Array.sub(arr, x) = 0
then Array.update(arr, x, Array.sub(arr, x)+1)
else Array.update(arr, x, Array.sub(arr, x)+1);
if Array.sub(arr, x)-1 < 1
then Ping(arr, (K-1), xs, (x::ping_list))
else Ping(arr, K, xs, (x::ping_list)))
and Pong(arr, [], _) = []
|Pong(arr, (x::xs), min) =
(if Array.sub(arr, x) >= 1
then (Array.update(arr, x, Array.sub(arr, x)-1); [])
else [];
if Array.sub(arr,x) + 1 > 1
then if (length xs) < min then Pong(arr, xs, (length xs)) else Pong(arr, xs, min)
else xs);
val result = Ping(arr, 3, [0, 2, 4], []);
val it = arr;
输出:
val arr = [|0,0,0,0,0,0,0,0,0,0|] : int array
val Ping = fn : int array * int * int list * int list -> bool
val Pong = fn : int array * int list * int -> int list
val result = true : bool
val it = [|1,0,1,0,1,0,0,0,0,0|] : int array
我之前解决的问题 运行 它:
- Pong return 类型不一致(混合
()
和 int list
)
- Ping 和 Pong 没有相互定义
- Pong
的混合咖喱和非咖喱 definition/calls
假设我有两个 SML 函数 Ping 和 Pong.My 代码是这样的:
val arr=Array.array(10,0)
fun Ping (arr,_,[],_)=true
|Ping (arr,0,(x::xs),ping_list)=Ping(arr,1,xs,Pong(arr,ping_list,10)) //<-Problem is here
| Ping (arr,K,(x :: xs),ping_list) =
(if Array.sub(arr,x)=0
then
Array.update(arr, x, Array.sub(arr,x)+1)
else
Array.update(arr, x, Array.sub(arr,x)+1);
if Array.sub(arr,x)-1<1 then Ping (arr,(K-1),xs,(x::ping_list)) else Ping (arr,K,xs,(x::ping_list)))
fun Pong arr [] _ =[]
|Pong arr (x::xs) min=
(if Array.sub(arr,x)>=1
then Array.update(arr,x,Array.sub(arr,x)-1)
else();
if Array.sub(arr,x)+1>1
then if (length xs)<min then Pong arr xs (length xs)
else Pong arr xs min
else xs)
如果我用数组 3:int,[1,2,3]:list,[] 调用 Ping,如果我用array,list,integer 它给我输出 i want.In ping funcrion 如果 K=0 我想调用 pong 函数(修改数组)并递归调用 Ping 函数但是在参数 ping_list 中有Pong 的结果(这是一个列表)但是 运行 这给了我这个错误:
stdIn:27.40-27.73 Error: operator and operand do not agree [tycon mismatch]
operator domain: int array
operand: 'Z * 'Y * [int ty]
in expression:
Pong (arr,ping_list,10)
我也尝试过将 Ping 和 Pong 放在本地并从中调用 Ping,但似乎没有 work.How 这可以解决吗?
Control.Print.printDepth := 20;
val arr = Array.array(10,0)
fun Ping (arr, _, [], _) = true
|Ping (arr, 0, (x::xs), ping_list) = Ping(arr, 1, xs, Pong(arr, ping_list, 10))
|Ping (arr, K, (x :: xs), ping_list) =
(if Array.sub(arr, x) = 0
then Array.update(arr, x, Array.sub(arr, x)+1)
else Array.update(arr, x, Array.sub(arr, x)+1);
if Array.sub(arr, x)-1 < 1
then Ping(arr, (K-1), xs, (x::ping_list))
else Ping(arr, K, xs, (x::ping_list)))
and Pong(arr, [], _) = []
|Pong(arr, (x::xs), min) =
(if Array.sub(arr, x) >= 1
then (Array.update(arr, x, Array.sub(arr, x)-1); [])
else [];
if Array.sub(arr,x) + 1 > 1
then if (length xs) < min then Pong(arr, xs, (length xs)) else Pong(arr, xs, min)
else xs);
val result = Ping(arr, 3, [0, 2, 4], []);
val it = arr;
输出:
val arr = [|0,0,0,0,0,0,0,0,0,0|] : int array
val Ping = fn : int array * int * int list * int list -> bool
val Pong = fn : int array * int list * int -> int list
val result = true : bool
val it = [|1,0,1,0,1,0,0,0,0,0|] : int array
我之前解决的问题 运行 它:
- Pong return 类型不一致(混合
()
和int list
) - Ping 和 Pong 没有相互定义
- Pong 的混合咖喱和非咖喱 definition/calls