Quantcast
Viewing latest article 1
Browse Latest Browse All 518

Why is the parameter implicitly declared in ocaml

I am learning OCaml to get into functional programming. I am solving the beginner exercises.

This is the solution of the exercise to get the nth element in a list.

let rec at k = function    | [] -> None    | h :: t -> if k = 0 then Some h else at (k - 1) t;;

val at : int -> 'a list -> 'a option = <fun>

I solved it myself like this which raises the following error

let rec at i l = function| [] -> None| x::y -> if i = 0 then Some x else at (i-1) y;;
Error: This expression has type 'a list -> 'a option       but an expression was expected of type 'a option  Hint: This function application is partial, maybe some arguments are missing.

The only difference i see is that the argument that is a list is implicit, but why??

That is an int should be correctly inferenced because I compare it with an int.


Viewing latest article 1
Browse Latest Browse All 518

Trending Articles