September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1,28 @@
import Control.Applicative (liftA2)
nthRoot :: Double -> Double -> Double
nthRoot n x =
fst $
until
(uncurry (==))
(((,) <*> ((/ n) . liftA2 (+) ((n - 1) *) ((x /) . (** (n - 1))))) . snd)
(x, x / n)
-- TESTS --------------------------------------------------
main :: IO ()
main =
putStrLn $
fTable
"Nth roots:"
(\(a, b) -> show a ++ " `nthRoot` " ++ show b)
show
(uncurry nthRoot)
[(2, 2), (5, 34), (10, 734 ^ 10), (0.5, 7)]
-- FORMAT OF RESULTS --------------------------------------
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
fTable s xShow fxShow f xs =
let w = maximum (length . xShow <$> xs)
rjust n c = liftA2 drop length (replicate n c ++)
in unlines $
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs