Question

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 whould I do to be able to pass the empty list as argument without triggering a value restriction error ?

Was it helpful?

Solution

I think you're going to have to put a type annotation somewhere, either on the empty list (as you have) or on the result of the call to last: (last [] : int).

OTHER TIPS

You can do

last<obj> []

But fsi will give you a slapped wrist because last never explicitly declare it's type parameter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top