0

I'm trying to pass a function as parameter to another function but I get an error at compilation.

Error: This function has type 'a -> 'a -> int
       It is applied to too many arguments; maybe you forgot a `;'.

My code is:

let sort to_do my_list =
        let rec change to_do my_list verify newlist = match my_list with
                | Empty -> if verify = newlist then newlist else
                           change to_do newlist newlist Empty
                | Item (elem, next) -> change to_do my_list verify (compare to_do elem newlist)
        in
        change to_do my_list my_list Empty
;;

let rec compare to_do elem my_list = match my_list with
        | Empty -> (elem, my_list)
        | Item (elem1, Empty) -> if to_do elem elem1 > 0 then Item (elem, elem1) else
                                  Item (elem1, elem)
        | Item (elem1, next) -> compare to_do elem next
;;   
3
  • Can you include the definition of the type with Empty and Item variants? Commented Mar 30, 2017 at 9:04
  • type 'a my_list = Item of ('a * 'a my_list) | Empty ;; Commented Mar 30, 2017 at 9:15
  • Empty is an exivalent of NULL or [] Commented Mar 30, 2017 at 9:15

2 Answers 2

2

In sort, you are referring to a function named compare. Since the other function compare is below, that will be resolved to Pervasives.compare : 'a -> 'a -> int (which takes two values and compare them).

Moving your compare function above sort may fix the problem.

Sign up to request clarification or add additional context in comments.

Comments

0

For the person interesting my final code is :

 let rec comp to_do elem my_list memlist = match my_list with
         | Empty -> Item (elem, memlist)
         | Item (elem1, Empty) -> if to_do elem1 elem > 0 then Item (elem, memlist) else
                                  rev (Item (elem, (rev memlist)))
         | Item (elem1, next) -> comp to_do elem next memlist
 ;;                                                                                                                                                                                           

 let sort to_do my_list =
         let rec change to_do my_list verify newlist = match my_list with
                 | Empty -> if verify = newlist then newlist else
                            change to_do newlist newlist Empty
                 | Item (elem, next) -> change to_do next verify (comp to_do elem newlist newlist)
         in
         change to_do my_list my_list Empty
 ;;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.