Using a list as arguments for a function in OCaml
<p>I am currently trying to learn OCaml. And I am searching for the equivalent of this python code:</p>

<pre><code>f(*l[:n])
</code></pre>

<p>I thought I'd try to write a function that emulates this behavior, but it doesn't work. Here is the code:</p>

<pre><code>let rec arg_supply f xs n =
    if n = 0 then
        f
    else
        match xs with
        | x :: remainder -&gt; arg_supply (f x) remainder (n - 1);;
</code></pre>

<p>And here is the error message I get:</p>

<pre><code>Error: This expression has type 'a but an expression was expected of type
'b -&gt; 'a
The type variable 'a occurs inside 'b -&gt; 'a
</code></pre>

<p>Any help is appreciated, be it a way to get my function working, or another way to supply the first n elements of a list to a function as arguments.</p>

<p>Edit: <code>n</code> is the amount of arguments needed to call the function, and constant because of it.</p>

<p>Edit: This one doesn't work either.</p>

<pre><code>type ('a, 'r) as_value = ASFunction of ('a -&gt; ('a, 'r) as_value) | ASReturnValue of 'r;;

let rec _arg_supply f_or_rval xs n =
    if n = 0 then
        f_or_rval
    else
        match f_or_rval with
        | ASFunction func -&gt; (
            match xs with
                | x :: r -&gt;  _arg_supply (func x) r (n - 1)
                | _ -&gt; failwith "too few arguments for f"
        )
        | ASReturnValue out -&gt; out;;
</code></pre>

<p>Your Python function <code>f</code> is being passed different numbers of arguments, depending on the value of <code>n</code>. This can't be expressed in OCaml. Functions take a statically fixed number of arguments (i.e., you can tell the number of arguments by reading the source code).</p>

<p>The way to pass different numbers of arguments to an OCaml function is to pass a list, corresponding to the Python code <code>f(l[:n])</code>.</p>

<p>(It's more accurate to say that an OCaml function takes one argument, but this is a discussion for another time.)</p>

<p><strong>Update</strong></p>

<p>If <code>n</code> is actually a constant, let's say it's 3. Then you can do something like the following:</p>

<pre><code> match l with
 | a :: b :: c :: _ -&gt; f a b c
 | _ -&gt; failwith "too few arguments for f"
</code></pre>

<p><strong>Update 2</strong></p>

<p>Here's another way to look at what you want to do. You want to write a function, let's call it <code>apply</code>, that works like this:</p>

<pre><code>let apply f xs =
    . . .
</code></pre>

<p>The idea is that <code>apply</code> calls the function <code>f</code>, giving it arguments from the list <code>xs</code>.</p>

<p>OCaml is a strongly typed language, so we should be able to give a type for <code>f</code> and for <code>xs</code>. What is the type of <code>f</code>? You want it to be a function of <code>n</code> arguments, where <code>n</code> is the length of <code>xs</code>. I.e., <code>n</code> is not a constant! But there is no such type in OCaml. Any function type has a fixed, static number of parameters. Since there's no OCaml type for <code>f</code>, you can't write the function <code>apply</code>. You can write a series of functions <code>apply1</code>, <code>apply2</code>, and so on. Note that each of these functions has a different type. If you don't mind calling the correct one for each different function <code>f</code>, that would work.</p>

<p>It's fairly likely that you can restructure your problem to work with OCaml's typing rather than struggling with it. I stand by my comments on strong typing: once you get used to strong typing, it's very hard to give up the benefits and go back to languages with little (or no) typing support.</p>

python ocaml