
I am writing some code to find peaks in molecular spectra. I represent a
spectrum as a list of numbers (intensities) and build a binary search tree
of the positions in the list (x-values) sorted by intensity.
Peaks in the spectrum appear as branches of the tree. My task is to return
branches of a binary tree that contain no other branches larger than some
size.
I wrote this function to test whether a tree can be classified as a peak.
I suspect it's very inefficient, and it doesn't seem idiomatic to me
either. I'm looking for suggestions to improve it:
-- |Returns True if the tree can be classified as a peak. Peaks are
-- qualified by not having any two branches with a size greater than n.
isPeak :: Int -> Tree a -> Bool
isPeak _ Tip = True
isPeak n (Node _ l r)
| (ltn l) && (ltn r) = True
| (ltn l) && (gtn r) = isPeak n r
| (gtn l) && (ltn r) = isPeak n l
| (gtn l) && (gtn r) = False
| otherwise = error "isPeak: no matching condition in guard"
where gtn = (>=n) . size
ltn = (

On Fri, Jul 06, 2007 at 11:47:58AM -0400, Kyle L Bittinger wrote:
I am writing some code to find peaks in molecular spectra. I represent a spectrum as a list of numbers (intensities) and build a binary search tree of the positions in the list (x-values) sorted by intensity.
In general, the best way to implement binary search trees in Haskell is: -- http://haskell.org/ghc/dist/current/docs/libraries/base/Data-Set.html import qualified Data.Set as S If you can avoid depending on the details of balancing... :(
Peaks in the spectrum appear as branches of the tree. My task is to return branches of a binary tree that contain no other branches larger than some size.
I wrote this function to test whether a tree can be classified as a peak. I suspect it's very inefficient, and it doesn't seem idiomatic to me either. I'm looking for suggestions to improve it:
Stefan
participants (2)
-
Kyle L Bittinger
-
Stefan O'Rear