[Haskell-beginners] maybe this could be improved?

Michael Mossey mpm at alumni.caltech.edu
Thu Nov 12 17:31:29 EST 2009


Hi, Thanks for your help and it looks like you identified some conditions 
that could be removed. There is one change necessary, I think.

closest a b = if (inPitch - a) < (b - inPitch) then a else b

Patrick LeBoutillier wrote:
> Michael,
> 
> Here's my stab at it, not sure if it's really better though:
> 
> 
> findClosestPitch :: SampleMap -> Pitch -> ErrorT String Identity Pitch
> findClosestPitch samples inPitch = do
>   when (M.null samples) $ throwError "Was given empty sample table."
>   case M.splitLookup inPitch samples of
>     (_, Just _, _) -> return inPitch
>     (ml, _, mh)    -> return $ approxPitch ml mh
>     where
>       approxPitch ml mh | M.null ml = fst . M.findMin $ mh
>       approxPitch ml mh | M.null mh = fst . M.findMax $ ml
>       approxPitch ml mh             = closest (fst . M.findMax $ ml)
> (fst . M.findMin $ mh)
>         where closest a b = min (inPitch - a) (b - inPitch)
> 
> 
> I tried to separate the approximation part from the rest of the code,
> and used a bit of deduction to eliminate (hopefully correctly...) some
> of the testing conditions.
> Anyways, I had fun doing working on this, and I learned a bit about
> computerized music as well!
> 
> 
> Thanks,
> 
> Patrick
> 
> 
> 


More information about the Beginners mailing list