You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.1 KiB

open System
let readStringList () =
let rec readLines acc =
let line = Console.ReadLine()
if String.IsNullOrEmpty(line) then
List.rev acc
else
readLines (line :: acc)
readLines []
let sortByLength strings =
strings |> List.sortBy (fun s -> s.Length)
let printStringList strings =
strings |> List.iter (fun s -> printfn "%s" s)
let demonstrateWithSample () =
let sampleStrings = ["hello"; "world"; "a"; "programming"; "test"]
printfn "Original list:"
printStringList sampleStrings
printfn "\nSorted by length:"
let sorted = sortByLength sampleStrings
printStringList sorted
[<EntryPoint>]
let main argv =
printfn "Enter strings (empty line to finish):"
let strings = readStringList ()
if List.isEmpty strings then
printfn "No strings entered. Using sample data:"
demonstrateWithSample ()
else
printfn "\nOriginal strings:"
printStringList strings
printfn "\nSorted by length:"
let sortedStrings = sortByLength strings
printStringList sortedStrings
0