10.07.2015 Views

Recursion Patterns and Time-analysis - Departamento de ...

Recursion Patterns and Time-analysis - Departamento de ...

Recursion Patterns and Time-analysis - Departamento de ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The complete implementation of mergesort as a hylomorphism is given below:hyloMs = (cataLTree ms_merge wrap) . (anaLTree f)The rea<strong>de</strong>r will have noticed that there is a slight mismatch between the initial <strong>de</strong>finition of mergesort <strong>and</strong> thishylomorphism, since the latter is not <strong>de</strong>fined on the empty list. This can be easily solved by using a Maybe type forthe leaves; we will however stick to this partial version for the sake of simplicity.Quicksort. In or<strong>de</strong>r to find out what the catamorphism <strong>and</strong> anamorphism functions will be like for quicksort, it isfirst necessary to i<strong>de</strong>ntify a suitable intermediate data structure, as was done for mergesort. The implementation ofquicksort in Haskell is straightforward, following the classic <strong>de</strong>finition of the algorithm:qs [] = []qs (h:t) = let (a,b) = qs_split h tin (qs a) ++ h:(qs b)qs_split _ [] = ([],[])qs_split p (h:t) = let (a,b) = qs_split p t in if h

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!