Passing (the output of) one function straight into another

I am currently just starting out with OCaml and am working through this tutorial:

Inside the utop REPL,
We’ve defined a recursive function range that takes two int lo and hi and returns a list from lo to hi

let rec range lo hi =
    if lo > hi then
      []
    else
      lo :: range (lo + 1) hi;;

Later, in the same REPL session we define another function sum that uses pattern matching to take a list (of ints) and return the sum:

let rec sum u =
    match u with
    | [] -> 0
    | x :: v -> x + sum v;;

So, I naively think, why not take the range function, give it two ints to generate a list then pass that list straight into sum?

sum range 1 10;;
Error: The function 'sum' has type int list -> int
       It is applied to too many arguments
Line 1, characters 10-11:
  This extra argument is not expected.

I feel like it’s probably some syntactic thing that’s missing since, I believe anyway, the types of the inputs and outputs should match up - but I’m quite open to being shown where I’m wrong.

Thanks in advance.

2 Likes

sum (range 1 10) should do the trick!

1 Like

Indeed it does, thank you for your (incredibly) quick reply. I’d like to point out that earlier in this tutorial I was clearly promised no parentheses though! (kidding). Thanks again.

1 Like

No problem! :slight_smile: A couple ways you could do it without parens:

let lst = range 1 10 in
sum lst

or

sum @@ range 1 10
1 Like