Area Under Curves and Volume of Revolving a Curve Discussions | Functional Programming | HackerRank
We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
{-# LANGUAGE ConstraintKinds, DeriveFunctor #-}importText.Printf(printf)importData.Foldable(fold)importData.Bifunctor(bimap)importData.Monoid((<>))typePolynomialAblepownum=(Integralpow,Floatingnum)dataMonomialpow=Ln|ToThepowderiving(Functor,Eq,Show)newtypePolynomialpownum=P{getTerms::[(Monomialpow,num)]}deriving(Eq,Show)sqrPoly::PolynomialAblepownum=>Polynomialpownum->PolynomialpownumsqrPoly(Pp)=P$concatMap(\(ToThek,v)->bimap(fmap(+k))(*v)`map`p)ptoFn::PolynomialAblepownum=>Polynomialpownum->(num->num)toFn=foldr(\(n,c)fn->\y->letf=casenof{ToThen'->(^^n');Ln->log}infny+c*fy)(const0).getTermssolve::Double->Double->PolynomialIntDouble->(Double,Double)solvelrp=(area,volume)wherearea=integrateplrvolume=pi*integrate(sqrPolyp)lrmain::IO()main=getContents>>=putStrLn.uncurry(cojoin$printf"%.1f\n").(\(a:b:(l:r:_):_)->solvelr.P$zip(map(ToThe.floor)b)a).map(mapread.words).lineswherecojoinf=\xx'->fx<>fx'integrate::PolynomialAblepownum=>Polynomialpownum->num->num->numintegratecoefslboundrbound=capFrbound-capFlboundwherecapF=toFn$reversePowerRulecoefsreversePowerRule::PolynomialAblepownum=>Polynomialpownum->PolynomialpownumreversePowerRule=P.map(uncurryaux).getTermsaux(ToTheidx)c|idx==-1=(Ln,c)|otherwise=letidx'=idx+1in(ToTheidx',c/fromIntegralidx')
Could be much prettier
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Area Under Curves and Volume of Revolving a Curve
You are viewing a single comment's thread. Return to all comments →
Could be much prettier