2016-08-04 2 views
3

同意、および合計以下にリストの最初のn個の要素を追加するようなint型のnを返しますが、しないでくださいリストの最初のn + 1要素が合計以上に加算されます。標準MLエラー:演算子とオペランドは、私はサムと呼ばれるint型をとる関数<code>number_before_reaching_sum</code>を書きたい

hw1_1.sml:67.14-67.97 Error: operator and operand don't agree [type mismatch] 
    operator domain: {1:'Y; 'Z} 
    operand:   int * int list * int -> 'X 
    in expression: 
    (fn {1=1,...} => 1) list_compute 
hw1_1.sml:67.14-67.97 Error: operator and operand don't agree [type mismatch] 
    operator domain: {2:'Y; 'Z} 
    operand:   int * int list * int -> 'X 
    in expression: 
    (fn {2=2,...} => 2) list_compute 
hw1_1.sml:69.11-69.44 Error: operator and operand don't agree [type mismatch] 
    operator domain: {2:'Y; 'Z} 
    operand:   int * int list * int -> int * int 
    in expression: 
    (fn {2=2,...} => 2) list_compute 

(#1 list_compute(sum_list2,tl lists2,n+1),#2 list_compute(sum_list2,tl lists2,n+1))#2 list_compute(sum_list,lists,n) この2行が間違っている理由を私は理解することはできません。 はここでエラーメッセージがプリントアウトし、私のコード

fun number_before_reaching_sum(sum:int,lists:int list)= 
let 
val sum_list=0 
val n=0 
in 
    let fun list_compute(sum_list:int,lists2:int list,n:int)= 
      let val sum_list2=sum_list+(hd lists2) 
      in if sum_list2>=sum 
        then (sum_list2,n+1) 
       else (#1 list_compute(sum_list2,tl lists2,n+1),#2 list_compute(sum_list2,tl lists2,n+1)) 
       end 
    in #2 list_compute(sum_list,lists,n) 
    end 
end 

です。

+0

一般的に、標準MLでこのようなエラーが発生した場合は、式を括弧で囲むのを忘れたからです。 –

答えて

5

f g(x,y)(f g) (x,y)、ないf (g (x,y))として解析されます。それ以外の場合は、機能list_compute#1を適用しようと

#1 (list_compute (sum_list2,tl lists2,n+1)) 

:だから、このような括弧を追加します。エラーメッセージは、 "#1はタプルが必要ですが、代わりに関数を与えました"というコンパイラです。

関連する問題