JLFG4356
JLFG4356

Reputation: 13

Ocaml function wanting to take 2 arguments although just one is necessary?

I'm trying to write a function that finds the longest running sub-list of increasing integers in a list.

This is what I have so far (very long variable names to help me understand it better):

let increasing list = 
  let rec aux inputList currentLongestList currentHighestList lastInsertedElement = 
    match inputList with
    | [] -> 
      if List.length currentLongestList > List.length currentHighestList then currentLongestList
      else currentLongestList
    | hd :: tl -> 
      if hd > lastInsertedElement then aux tl (currentLongestList @ hd) currentHighestList hd
      else aux tl currentLongestList currentHighestList lastInsertedElement
  in 
  aux list [] [] [min_int]
;;

My issue is that when this is entered, it says val increasing : int list list -> int list = <fun> implying 2 lists need to be passed where I only want one in the function? What am I doing wrong? Also, the function doesn't work if I take away the square brackets from min_int, even though I don't see why they're needed - what would I have to do to get rid of them?

Upvotes: 1

Views: 170

Answers (1)

Yann R&#233;gis-Gianas
Yann R&#233;gis-Gianas

Reputation: 111

The inferred type informs you that the argument named list is a list of lists of integers. Let us understand why, step by step.

First, the type checker has unified the type list with the type of inputList because of your call to aux at the end of the function increasing.

Second, the head of the aux argument named inputList is compared with lastInsertedElement in the second case of aux pattern-matching. Since lastInsertedElement is of type int list because of your initial call to aux, the type induces that inputList has type int list list.

For your second question, "why the brackets seem needed around min_int?". Look at the expression "... @ hd". It implies that "hd" must be a list...

Upvotes: 1

Related Questions