September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
28
Task/Nth-root/Haskell/nth-root-2.hs
Normal file
28
Task/Nth-root/Haskell/nth-root-2.hs
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue