kokuyouwind/haskell-exercises chapter4の解答
-- 再帰(chapter4)
tri_number :: Int -> Int
tri_number n
| n == 0 = 0
| otherwise = n + tri_number (n-1)
tetration :: Integer -> Integer -> Integer
tetration n m
| m == 0 = 1
| otherwise = (n^) $ tetration n (m-1)
index :: Int -> [a] -> a
index n (x:xs)
| 0 > n = error "index outbounded"
| length xs < (n-2) = error "index outbounded"
| n == 0 = x
| otherwise = index (n-1) xs
even_odd :: [Int] -> ([Int], [Int])
even_odd [] = ([], [])
even_odd (x:xs)
| even x = (fst (even_odd xs), x:snd (even_odd xs))
| otherwise = (x:fst (even_odd xs), snd (even_odd xs))
-- 挿入ソート
insert :: Ord a => [a] -> a -> [a]
insert [] n = [n]
insert xs n = let xss = span (<n) xs
in fst xss ++ [n] ++ snd xss
isort :: Ord a => [a] -> [a]
isort [] = []
isort (x:xs) = insert (isort xs) x
-- 分割数
part_num :: Int -> Int
part_num n = part_num' n 1
part_num' :: Int -> Int -> Int
part_num' n m
| n == m = 1
| n-m >= m = (part_num' n (m+1)) + part_num' (n-m) m
| otherwise = part_num' n (m+1)