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 accomplish 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?
So what would the code look like? This would certainly work: