module Chapter05 where
import Data.List (find)
sndItem0 :: [a] -> a
sndItem0 :: forall a. [a] -> a
sndItem0 [a]
as =
if forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
as
then forall a. HasCallStack => [Char] -> a
error [Char]
"Empty list has no second element."
else
if forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
as forall a. Eq a => a -> a -> Bool
== Int
1
then forall a. HasCallStack => [Char] -> a
error [Char]
"1-item list has no 2nd item."
else [a]
as forall a. [a] -> Int -> a
!! Int
1
repeatInt :: Int -> [Int]
repeatInt :: Int -> [Int]
repeatInt = forall a. a -> [a]
repeat
null' :: [a] -> Bool
null' :: forall a. [a] -> Bool
null' [a]
as = forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
as forall a. Eq a => a -> a -> Bool
== Int
0
last' :: [a] -> a
last' :: forall a. [a] -> a
last' = forall a. [a] -> a
head forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [a]
reverse
palindrome :: String -> Bool
palindrome :: [Char] -> Bool
palindrome [Char]
as = forall a. [a] -> [a]
reverse [Char]
as forall a. Eq a => a -> a -> Bool
== [Char]
as
cycle' :: [a] -> [a]
cycle' :: forall a. [a] -> [a]
cycle' [] = forall a. HasCallStack => [Char] -> a
error [Char]
"empty list"
cycle' [a]
as = forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ forall a. a -> [a]
repeat [a]
as
type R = Double
euler :: R
euler :: R
euler = forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [R
1 forall a. Fractional a => a -> a -> a
/ forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
n forall a. Floating a => a -> a -> a
** R
2 | Integer
n <- [Integer
1 .. Integer
100]]
pi' :: R
pi' :: R
pi' = forall a. Floating a => a -> a
sqrt forall a b. (a -> b) -> a -> b
$ R
euler forall a. Num a => a -> a -> a
* R
6
fact :: Integer -> Integer
fact :: Integer -> Integer
fact Integer
n = forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
product [Integer
n, (Integer
n forall a. Num a => a -> a -> a
- Integer
1) .. Integer
1]
expList :: R -> [R]
expList :: R -> [R]
expList R
x = [(R
1 forall a. Num a => a -> a -> a
+ R
x forall a. Fractional a => a -> a -> a
/ R
n) forall a. Floating a => a -> a -> a
** R
n | R
n <- [R
1, R
2 ..]]
calcSeriesLengthWithinPercent :: (R -> [R]) -> (R -> R) -> R -> R -> Maybe Int
calcSeriesLengthWithinPercent :: (R -> [R]) -> (R -> R) -> R -> R -> Maybe Int
calcSeriesLengthWithinPercent R -> [R]
seriesFn R -> R
actFn R
pct R
x =
forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\(R, Int)
tup -> forall a b. (a, b) -> a
fst (R, Int)
tup forall a. Ord a => a -> a -> Bool
>= (R -> R
actFn R
x forall a. Num a => a -> a -> a
* ((R
100 forall a. Num a => a -> a -> a
- R
pct) forall a. Fractional a => a -> a -> a
/ R
100))) (forall a b. [a] -> [b] -> [(a, b)]
zip (R -> [R]
seriesFn R
x) [Int
1 ..])
expSeries :: R -> [R]
expSeries :: R -> [R]
expSeries R
x =
let terms :: Integer -> [R]
terms :: Integer -> [R]
terms Integer
i =
[ R
x forall a b. (Num a, Integral b) => a -> b -> a
^ Integer
m forall a. Fractional a => a -> a -> a
/ forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> Integer
fact Integer
m)
| Integer
m <- [Integer
0 .. Integer
i]
]
in [forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum forall a b. (a -> b) -> a -> b
$ Integer -> [R]
terms Integer
i | Integer
i <- [Integer
1 ..]]