F # select sort by random integer list

I am new to F # and trying to figure out some basics, but slowly stumble. In the code below, I am trying to create a list of random integers and then sort it.

let randomNumberList count =
    let r = System.Random()
    List.init count (fun _ -> r.Next(100))

let rec selectionSort l = function
    | [] -> []
    | l -> let min = List.min l in
           let rest = List.filter (fun i -> i <> min) l in
           let sortedList = selectionSort rest in
           min :: sortedList
sortedList = selectionSort l

let unsortedList = randomNumberList 10
printfn "%A" unsortedList
printfn "%A" sortedList

So two things ... one error I get:

stdin(515,19): error FS0001: This expression was expected to have type
    'a list    
but here has type
    'a list -> 'a list

The other is with a list of random numbers. It works, but I want the numbers created less than 100, instead of the massive values ​​that I get now.

Thank you for your patience and help!

+3
source share
1 answer

As indicated in the comment, there is no need lfor let rec selectionSort. Fixed code:

let randomNumberList count =
    let r = System.Random()
    List.init count (fun _ -> r.Next(100))

let rec selectionSort = function
    | [] -> []
    | l -> let min = List.min l in
           let rest = List.filter (fun i -> i <> min) l in
           let sortedList = selectionSort rest in
           min :: sortedList

let unsortedList = randomNumberList 10
let sortedList = selectionSort unsortedList
printfn "%A" unsortedList
printfn "%A" sortedList
System.Console.ReadLine() |> ignore

Explanation:

let vname = function
   | ... -> ...
   | ... -> ...

- short form

let vname arg = match arg with
   | ... -> ...
   | ... -> ...
+1
source

All Articles