开发者

Trying to learn F#...sort list of integers

I've been using Python the past couple of months and now am trying to give F# a whirl. Only...I don't really get it. I've been reading documentation for the past few days and still don't completely understand how to acc开发者_JAVA百科omplish basic tasks.

I've been following the tutorials on tryfsharp.org and fsharp.net.

For instance, how would I accomplish this basic task written in Python, in F# instead?

unsorted = [82, 9, 15, 8, 21, 33, 4, 89, 71, 7]
sorted = []
for n in range(1,len(unsorted)):
    lowest = 0
    for i in range(0,len(unsorted)-1):
        if unsorted[i] < unsorted[lowest]:
            lowest = i
    sorted.append(unsorted[lowest])
    del unsorted[lowest]
print sorted


When porting code from an imperative language to a functional language, you should try to convert the algorithm that is used in the code, rather than the code itself IMHO.

The code is doing a selection sort so you want to ask yourself, what does the selection sort do?

  • find the minimum
  • put it in the front of the sorted list.
  • sort the rest of the items placing the results after the minimum.

So what would the code look like? This would certainly work:

let rec selection_sort = function
    | [] -> []
    | l -> let min = List.min l in                         (* find the minimum *)
           let rest = List.filter (fun i -> i <> min) l in (* find the rest *)
           let sorted_rest = selection_sort rest in        (* sort the rest *)
           min :: sorted_rest                              (* put everything together *)


Notice that your python version is incorrect. It outputs:

[4, 8, 9, 15, 21, 33, 71, 82, 89]

Lacks 7.

Here is a direct F# translation:

let unsorted = new ResizeArray<int> ([| 82; 9; 15; 8; 21; 33; 4; 89; 71; 7 |])
let sorted = new ResizeArray<int> ()
for n=1 to unsorted.Count-1 do
    let mutable lowest = 0
    for i=0 to unsorted.Count-1 do // i changed this line so the output is correct. 
        if unsorted.[i] < unsorted.[lowest] then
            lowest <- i
    sorted.Add(unsorted.[lowest])
    unsorted.RemoveAt(lowest)

printfn "%A" (sorted |> Seq.toArray)

The translated version is nearly exactly the same as the Python one. But this is not the ideal way to write F# programs. For sorting algorithms in F#, you can read a blog post on my blog:

http://fdatamining.blogspot.com/2010/03/test.html


I realize that this might not be exactly what your looking for if you want a direct translation, but F# and functional programming tend to emphasize declarative programming more than imperative languages. For example, if you want to sort a list of numbers, just sort them:

let unsorted = [2; 9; 15; 8; 21; 33; 4; 89; 71; 7]
let sorted = unsorted |> List.sort

//now print em out
sorted |> List.iter (printfn "%d")

If you're having trouble groking F#, it may be beneficial to read up on functional programming a bit to help you understand why F# does things differently. This article I wrote last year may help http://msdn.microsoft.com/en-us/magazine/ee336127.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜