0

I am writing a function that takes a function as input. This function also takes a function as input. I want to give labels to both those functions. I have tried:

let first (second: ((third: ('a -> 'b)) -> 'a )) : 'a =

but this gives me a syntax error "type expected" after the signature of third. What am I doing wrong?

1 Answer 1

3

You have some oddly placed parentheses here, which doesn't make a whole lot of sense (hence why the compiler complains), but from your description I believe you want this:

let first (second: (third: 'a -> 'b) -> 'a) : 'a = ...
val first : ((third:'a -> 'b) -> 'a) -> 'a = <fun>

But note also that second here is still not a labeled argument, just the name that the argument will be bound to in the definition. To make it labeled, you need to prefix it with ~:

let first ~(second: (third: 'a -> 'b) -> 'a) : 'a = ...
val first : second:((third:'a -> 'b) -> 'a) -> 'a = <fun>

The difference in notation is because second is not part of the type. You could also have written this as:

let first : (second:(third:'a -> 'b) -> 'a) -> 'a  = fun second -> ...
val first : (second:(third:'a -> 'b) -> 'a) -> 'a = <fun>
Sign up to request clarification or add additional context in comments.

4 Comments

First is the function I am writing. I want to use second and third in my function. If both 'a and 'b are int I want to use "let new = third 1" and "let newer = second thirdPrime"
You can't bind an argument that isn't in scope. The argument to third is something that will have to be provided when you call second, presumably in the definition of first.
How would I do that? Sorry but I'm rather new at this and OCaml is nothing like any other language I know.
I think a better question might be why you would do this. The type doesn't make a whole lot of sense to me, but it is valid.

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.