Domanda
Ho questo codice fornito da mio istruttore. Dovrei risolvere il problema trovando che tipo f # deduce da mergesort. Quando cerco di inviare interattivo ottengo un errore. Ho chiesto al mio Proffesor cosa non andava e mi ha detto che era a causa di errori di formattazione sul sito di classe. Ho provato ad aggiungere spazi rimozione di spazi è il nome, ma ogni volta che ho un
~ vs4489.fsx (8,14): l'errore FS0588: Block seguendo questo 'lasciare' è incompiuta. Aspettatevi un'espressione.
sulle ultime due metodi. Come posso risolvere questo problema?
Ecco il codice
let rec merge = function
| ([], ys) -> ys
| (xs, []) -> xs
| (x::xs, y::ys) -> if x < y then x :: merge (xs, y::ys)
else y :: merge (x::xs, ys)
let rec split = function
| [] -> ([], [])
| [a] -> ([a], [])
| a::b::cs -> let (M,N) = split cs
(a::M, b::N)
let rec mergesort = function
| [] -> []
| L -> let (M, N) = split L
merge (mergesort M, mergesort N)
Soluzione
posso solo indovinare di ciò che il rientro è corretto, ma ...
let rec merge = function
| ([], ys) -> ys
| (xs, []) -> xs
| (x::xs, y::ys) -> if x < y then x :: merge (xs, y::ys)
else y :: merge (x::xs, ys)
let rec split = function
| [] -> ([], [])
| [a] -> ([a], [])
| a::b::cs -> let (M,N) = split cs
(a::M, b::N)
let rec mergesort = function
| [] -> []
| L -> let (M, N) = split L
merge (mergesort M, mergesort N)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow