Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 595

Why is the parameter implicitly declared in ocaml

$
0
0

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 all articles
Browse latest Browse all 595

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>