From 5e74ab38894c8dd44b92a705017f31e928796ede Mon Sep 17 00:00:00 2001 From: Alexander Rukletsov Date: Fri, 2 Aug 2019 12:59:21 +0200 Subject: [PATCH] F# implementation. --- f#/StalinSort.fs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 f#/StalinSort.fs diff --git a/f#/StalinSort.fs b/f#/StalinSort.fs new file mode 100644 index 0000000..27cede2 --- /dev/null +++ b/f#/StalinSort.fs @@ -0,0 +1,31 @@ +open System + + module Seq = + let stalinSort s = + // `acc` must be non-empty + let rec impl acc rest = + if (Seq.isEmpty rest) then acc + elif (Seq.last acc) <= (Seq.head rest) then impl (Seq.append acc (Seq.singleton (Seq.head rest))) (Seq.tail rest) + else impl acc (Seq.tail rest) + + if (Seq.isEmpty s) then s + else impl (Seq.singleton (Seq.head s)) (Seq.tail s) + + +[] +let main argv = + let test s = + printfn "before:" + s |> Seq.iter (printf "%d ") + printfn "" + + printfn "after:" + (Seq.stalinSort s) |> Seq.iter (printf "%d ") + printfn "" + + test [1; 2; 4; 3; 8; 0; 9; 5; 7] + printfn "" + + test [1; 2; 4; 4; 3] + + 0