Domanda

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

Cosa dovrei fare per poter passare l'elenco vuoto come argomento senza innescare un errore di restrizione del valore?

È stato utile?

Soluzione

Penso che dovrai mettere un tipo di annotazione da qualche parte, nell'elenco vuoto (come hai fatto) o sul risultato della chiamata per durare: (last [] : int).

Altri suggerimenti

Tu puoi fare

last<obj> []

Ma FSI ti darà un polso schiaffeggiato perché ultimo non dichiara mai esplicitamente il parametro del tipo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top