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 开发者_如何学Go-> 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 ?
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)
.
You can do
last<obj> []
But fsi will give you a slapped wrist because last never explicitly declare it's type parameter.
精彩评论