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

Commit bc2b120

Browse files
feat: add 'Why Functional Programming Matters' section with README and Haskell examples, and update root README
1 parent 2f0a875 commit bc2b120

4 files changed

Lines changed: 140 additions & 12 deletions

File tree

README.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,16 @@
44

55
## INDEX
66

7-
- [sort](./sort/README.md)
8-
97
---
108

119
## ABOUT
1210

1311
基本アルゴリズムを学習するためのリポジトリですので途中過程など過剰なほどコメントを書いてます。
1412

15-
16-
---
17-
18-
## TARGET
19-
20-
- 各アルゴリズムの理解を深める。
21-
- 計算量を意識する。
22-
- 言語の特性を感じる。
23-
2413
---
2514

2615
## ENVIRONMENT
2716

2817
- python3
2918
- C/C++
30-
そのうち rust も追加するかもしれません。
19+
- Haskell
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
-- Haskell版
2+
sumList :: (Num a) => [a] -> a
3+
sumList [] = 0
4+
sumList (x : xs) = x + sumList xs
5+
6+
-- Haskellのfoldrと同じ
7+
reduce' :: (a -> b -> b) -> b -> [a] -> b
8+
reduce' _ x [] = x
9+
reduce' f x (a : l) = f a (reduce' f x l)
10+
11+
-- reduce版 sum
12+
sumList' :: (Num a) => [a] -> a
13+
sumList' xs = reduce' (+) 0 xs -- reduce'はfoldrで置き換え可能
14+
15+
product' :: (Num a) => [a] -> a
16+
product' xs = reduce' (*) 1 xs
17+
18+
-- Haskellにはandという関数がある
19+
allTrue :: [Bool] -> Bool
20+
allTrue xs = foldr (&&) True xs
21+
22+
-- Haskellにはorという関数がある
23+
anyTrue :: [Bool] -> Bool
24+
anyTrue xs = foldr (||) False xs
25+
26+
-- リストの連結を行う
27+
append' :: [a] -> [a] -> [a]
28+
append' xs ys = foldr (:) ys xs
29+
30+
-- リストの中身を2倍にする
31+
doubleall :: (Foldable t, Num a) => t a -> [a]
32+
doubleall xs = foldr doubleandcons [] xs
33+
where
34+
doubleandcons num xs = (:) (2 * num) xs
35+
36+
-- リストの中身を2倍にする(関数合成版)
37+
doubleall' :: (Foldable t, Num a) => t a -> [a]
38+
doubleall' xs = foldr ((:) . double) [] xs
39+
where
40+
double n = 2 * n
41+
42+
-- リストの中身を2倍にする(map + 関数合成版)
43+
doubleall'' :: (Foldable t, Num a) => t a -> [a]
44+
doubleall'' xs = map' double xs
45+
where
46+
map' f xs = foldr ((:) . f) [] xs
47+
double n = 2 * n
48+
49+
-- 2次元配列を全部足す
50+
sumMatrix :: [[a]] -> a
51+
sumMatrix xxs = (sum . map sum) xxs
52+
53+
main :: IO ()
54+
main = do
55+
-- リストの定義
56+
print $ 1 : (2 : (3 : [])) -- [1, 2, 3]
57+
58+
-- sum
59+
print $ sumList [1, 2, 3] -- 1 + 2 + 3 = 6
60+
61+
-- reduce版
62+
print $ sumList' [1, 2, 3]
63+
print $ product' [1, 2, 3] -- 1 * 2 * 3 = 6
64+
65+
-- リストの要素が全てTrueか
66+
print $ allTrue [True, True, True] -- True
67+
print $ allTrue [True, True, False] -- False
68+
69+
-- リストの要素が一つでもTrueか
70+
print $ anyTrue [True, True, True] -- True
71+
print $ anyTrue [True, True, False] -- True
72+
print $ anyTrue [False, False, False] -- False
73+
74+
-- リストの連結
75+
print $ append' [1, 2, 3] [4, 5]
76+
77+
-- リストの中身を2倍にする
78+
print $ doubleall [1, 2, 3]
79+
print $ doubleall' [1, 2, 3]
80+
81+
-- mapの発見
82+
print $ doubleall'' [1, 2, 3]
83+
print $ sumMatrix [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
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

WhyFunctionalProgrammingMatters/README.md

Whitespace-only changes.

0 commit comments

Comments
 (0)