一个函数调用另一个函数

时间:2019-04-06 17:10:00

标签: sml smlnj

假设我有两个SML函数Ping和Pong。我的代码是这样的:

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)

如果我用一个数组调用Ping,例如3:int,[1,2,3]:list,[],它会给我带来不希望的输出。以同样的方式,如果我用一个数组,列表调用Pong,整数给我我想要的输出。在ping函数中,如果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放在本地,然后从in调用Ping,但这似乎不起作用。如何解决?

1 个答案:

答案 0 :(得分:2)

  <script type="text/javascript">

    $(document).ready(function() {
        $('select[name="minfmname"]').on('change', function() {
            var matchID = $(this).val();
            if(matchID) {
                $.ajax({
                    /* change */ url: 'http://localhost/cd/index.php/matchcon/getteambymatch/'+matchID,
                    /* change*/ type: "GET",
                    dataType: "json",
                    success:function(teams) {
                        $('select[name="mteamid"]').empty();
        /* change*/                $.each(teams, function(key, value) {
                            $('select[name="mteamid"]').append('<option value="'+ value.team_id +'">'+ value.team_name +'</option>');
                        });
                    }
                });
            }else{
                $('select[name="mteamid"]').empty();
            }
        });
    });
</script>

输出:

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 ()
  • Ping和Pong没有相互定义
  • Pong的咖喱和非咖喱混合定义/呼叫