简体   繁体   中英

Using a list as arguments for a function in OCaml

I am currently trying to learn OCaml. And I am searching for the equivalent of this python code:

f(*l[:n])

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

let rec arg_supply f xs n =
    if n = 0 then
        f
    else
        match xs with
        | x :: remainder -> arg_supply (f x) remainder (n - 1);;

And here is the error message I get:

Error: This expression has type 'a but an expression was expected of type
'b -> 'a
The type variable 'a occurs inside 'b -> 'a

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.

Edit: n is the amount of arguments needed to call the function, and constant because of it.

Edit: This one doesn't work either.

type ('a, 'r) as_value = ASFunction of ('a -> ('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 -> (
            match xs with
                | x :: r ->  _arg_supply (func x) r (n - 1)
                | _ -> failwith "too few arguments for f"
        )
        | ASReturnValue out -> out;;

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

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

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

Update

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

 match l with
 | a :: b :: c :: _ -> f a b c
 | _ -> failwith "too few arguments for f"

Update 2

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

let apply f xs =
    . . .

The idea is that apply calls the function f , giving it arguments from the list xs .

OCaml is a strongly typed language, so we should be able to give a type for f and for xs . What is the type of f ? You want it to be a function of n arguments, where n is the length of xs . Ie, n 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 f , you can't write the function apply . You can write a series of functions apply1 , apply2 , 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 f , that would work.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM