Elimination of cost limitation errors

in OCaml

Objective Caml version 3.11.0

# let rec last l=
    match l with
    [] -> failwith("Empty list")
    |a::[] -> a
    |a::r -> last r;;
val last : 'a list -> 'a = <fun>
# last [];;
Exception: Failure "Empty list".

In f #

>let rec last l = 
    match l with
    [] -> failwith("Empty list")
    | a::[] -> a
    | a::r -> last r;;

val last : 'a list -> 'a

>last [];;
 last [];;
 ^^^^^^^

 stdin(8,1): error FS0030: Restriction de valeur....

>last ([]:int list);;

System.Exception: Empty list
   à FSI_0002.last[a](FSharpList`1 l)
   à <StartupCode$FSI_0003>.$FSI_0003.main@()
Arrêt en raison d'une erreur

What can I do to pass an empty list as an argument without causing a value constraint error?

+3
source share
2 answers

I think you have to put the annotation type somewhere, or an empty list (as applicable), or the result of the last call: (last [] : int).

+1
source

You can do

last<obj> []

But fsi will give you a wrist beat because the latter never explicitly declares it to be a type parameter.

0
source

All Articles