Welcome BrugerX!
So your implementation is actually spot on, and the only thing causing you trouble is syntax.
I actually get two errors with that code, and the two sort of help to explain the full situation. One error is as you said, and the other error is
This value is not a function and cannot be applied.
and the issue is here on this part
head::sortedInsert(x tail)
So what’s going on is that sortedInsert
is a function of 2 inputs, an int
, and an int list
, but when the compiler sees sortedInsert(x tail)
, it sees the function being applied just one input. There’s two problems with this, one is that (x tail)
makes the compiler mad, because it’s trying to call x
as a function and pass in tail
as input, which leads to one of the errors:
This value is not a function and cannot be applied.
Because of course x
cannot be called as a function. Then the 2nd issue is that when take sortedInsert : int -> int list -> int list
and call it just a single argument, you’re left with a function int list -> int list
. So if I pulled out the variable names and just left the types in, that’s kind of like saying
int :: (int list -> int list)
and then that’s where the other error message comes from. It’s expecting the right-hand side of ::
to be an int list
, since the left-hand side is just an int
, but the right hand side is actually an int list -> int list
, which is an invalid type.
So really, all this came about because you have some parentheses wrapped around the argument list. I would suggest trying to avoid extra unneeded parentheses when learning F# syntax, because they can throw you off sometimes like this. Here are some valid ways you could write it
head::sortedInsert x tail
head::(sortedInsert x tail)
head::sortedInsert (x) (tail)
head::(sortedInsert) (x) (tail)
head::(sortedInsert x) tail
head::(sortedInsert) x tail
(head::sortedInsert x tail)
but you stumbled across one of the invalid ways of writing it. In F# (as opposed to C# for example) parentheses have nothing at all to do with calling a function, and are only used for groupings.