Skip to content
This repository was archived by the owner on May 22, 2026. It is now read-only.

Commit 15eb585

Browse files
refactor(math): Organize math algorithms into dedicated directories
- Moved 'fibonacci', 'eratosthenes', and 'euclidean' implementations into their respective subdirectories within 'math/'. - Updated 'math/README.md' to reflect the new structure. - Added 'newtonRoot.hs' for Newton's method. - Added 'tree.hs' under 'data_structure/tree'.
1 parent d73a761 commit 15eb585

7 files changed

Lines changed: 119 additions & 0 deletions

File tree

data_structure/tree/tree.hs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
-- https://www.sampou.org/haskell/article/whyfp.html より
2+
data Tree a = Node a [Tree a]
3+
4+
-- 木のリストに対する畳み込み
5+
redtree ::
6+
(a -> b -> b) -> -- f : node を潰す
7+
(b -> b -> b) -> -- g : cons を潰す
8+
b -> -- a : nil を潰す
9+
Tree a ->
10+
b
11+
redtree f g a (Node label subtrees) =
12+
f label (redtree' f g a subtrees)
13+
14+
-- ツリーのリストを処理する関数
15+
redtree' ::
16+
(a -> b -> b) ->
17+
(b -> b -> b) ->
18+
b ->
19+
[Tree a] ->
20+
b
21+
redtree' f g a (subtree : rest) =
22+
g
23+
(redtree f g a subtree) -- リストのサイズが1に分解して潰す --> f label (redtree' f g a [先頭の木]) ...という流れでredtree' _ _ a [] = aにたどりつく
24+
(redtree' f g a rest) -- 残りで再帰
25+
redtree' _ _ a [] =
26+
a
27+
28+
tree :: Tree Int
29+
tree =
30+
Node
31+
1
32+
( (:)
33+
(Node 2 [])
34+
( (:)
35+
( Node
36+
3
37+
((:) (Node 4 []) [])
38+
)
39+
[]
40+
)
41+
)
42+
43+
sumtree :: (Num a) => Tree a -> a
44+
sumtree tree = redtree (+) (+) 0 tree
45+
46+
labels :: (Num a) => Tree a -> [a]
47+
labels tree = redtree (:) (++) [] tree
48+
49+
maptree :: (a -> b) -> Tree a -> Tree b
50+
maptree f tree = redtree (\label subtrees -> Node (f label) subtrees) (:) [] tree
51+
52+
main :: IO ()
53+
main = do
54+
print $ sumtree tree -- 10
55+
print $ labels tree -- [1, 2, 3, 4]
56+
print $ maptree (* 2) tree

math/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
# 数学ぽいやつ
22

3+
## フィボナッチ数列
4+
5+
[fibonacci](./fibonacci/)
6+
7+
---
8+
9+
## ニュートン法による平方根の近似
10+
11+
[newton](./newton/)
12+
13+
---
14+
315
## ユークリッドの互助法
416

17+
[euclidean](./euclidean/)
18+
19+
### 実装例
20+
21+
- [] C
22+
- [] Python
23+
- [x] Haskell
24+
25+
---
26+
527
## エラトステネスのふるい
28+
29+
[eratosthenes](./eratosthenes/)
30+
31+
- [] C
32+
- [] Python
33+
- [x] Haskell
34+
35+
---

math/newton/newtonRoot.hs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
-- https://www.sampou.org/haskell/article/whyfp.html
2+
-- 近似を1つ進める関数
3+
next :: Double -> Double -> Double
4+
next n x = (x + n / x) / 2
5+
6+
-- 許容誤差と近似値よりも差の小さい2つの連続する近似値を探す
7+
within :: Double -> [Double] -> Double
8+
within eps (a : b : rest)
9+
| abs (a - b) <= eps = b -- 絶対誤差
10+
| otherwise = within eps (b : rest)
11+
12+
-- 2つの近似値の比が1に近づくようにして許容誤差よりも小さい2つの連続する近似値を探す
13+
relative :: Double -> [Double] -> Double
14+
relative eps (a : b : rest)
15+
| abs (a - b) <= eps * abs b = b -- abs (a - b) / abs b <= epsを変形したもの。いわゆる相対誤差
16+
| otherwise = relative eps (b : rest)
17+
18+
-- withinを使って平方根をもとめる例
19+
sqrt' :: Double -> Double -> Double -> Double
20+
sqrt' a0 eps n = within eps $ iterate (next n) a0
21+
22+
relativesqrt :: Double -> Double -> Double -> Double
23+
relativesqrt a0 eps n = relative eps $ iterate (next n) a0
24+
25+
main :: IO ()
26+
main = do
27+
let n = fromIntegral 2 -- 平方根を求めたい値
28+
let a0 = fromInteger 1 -- 計算の初期値
29+
let eps = 0.001
30+
let epsRelative = 0.001
31+
print $ take 10 $ iterate (next n) 1
32+
print $ sqrt' a0 eps n
33+
print $ relativesqrt a0 epsRelative n

0 commit comments

Comments
 (0)