mooky Fare
mooky Fare

Reputation: 97

Standard ML error:operator and operand don't agree

I want to write a function number_before_reaching_sum that takes an int called sum, and return an int n such that the first n elements of the list add to less than sum, but the first n + 1 elements of the list add to sum or more. Here's my code

 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

The error message prints out:

    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

I cannot figure out why (#1 list_compute(sum_list2,tl lists2,n+1),#2 list_compute(sum_list2,tl lists2,n+1))and #2 list_compute(sum_list,lists,n) this 2 lines are wrong.

Upvotes: 3

Views: 438

Answers (1)

sepp2k
sepp2k

Reputation: 370092

f g(x,y) gets parsed as (f g) (x,y), not f (g (x,y)). So you want to add parentheses like this:

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

Otherwise it tries to apply #1 to the function list_compute. The error message is the compiler telling you "#1 wants a tuple, but you gave it a function instead".

Upvotes: 5

Related Questions