Date Sorting...

Ketil Z. Malde ketil@ii.uib.no
08 Mar 2002 08:40:13 +0100


"Michael Ruth" <meruth@hotmail.com> writes:

> I need to sort a list of dates provided in an input file. [...]

> --read in the lines one by one into a list
> --then iterate thru the list while breaking the strings into [Int] list
> -- of 3 elements [day,month,year]
> --then quicksort overall list
> --then print out overall list line by line

> I understand this is a imperative approach, but this is the way I was
> taught how to program, well that and OOP. How do I go about writing
> this in a functional sense?

I don't think it's either functional nor imperative by itself.  The
question is how you structure it, do you say something like

        buffer x
        list y

        x = readFile ...
        y = parse x
        quickSort y
        print y

(which would be imperative), or do you say something like

        print (quickSort (parse (readFile ...)))

(in Haskell you might prefer the equivalent syntax
        print $ quickSort $ parse $ readFile ...
or maybe
        sortDates = print . quickSort . parse . readFile
        sortDates ...
)

which would be functional.  Note that the only really imperative part
is the sorting, which actually changes the content of y for the
following part of the program.  A functional implementation wouldn't
destroy y, but create a new list with the elements from y in sorted
order.

I'm not sure if that made things clearer :-)

(If you really want to implement it, look at the Prelude for some
help, some of the things you wish to do is pretty standard fare.
And sorting is perhaps made easier if you rearrange the list of dates
a bit?) 

-kzm
-- 
If I haven't seen further, it is by standing in the footprints of giants