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

Commit 7d47606

Browse files
Merge pull request #13 from RyosukeDTomita/feature/WhyFunctionalProgramingMatters
feature/WhyFunctionalProgramingMatters
2 parents 2f0a875 + b054545 commit 7d47606

14 files changed

Lines changed: 297 additions & 28 deletions

File tree

.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use flake

.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
11
# all .out files
22
*.out
3+
input.txt
4+
inputs.txt
5+
.direnv/
6+
# https://github.com/github/gitignore/blob/main/Haskell.gitignore
7+
dist
8+
dist-*
9+
cabal-dev
10+
*.o
11+
*.hi
12+
*.hie
13+
*.chi
14+
*.chs.h
15+
*.dyn_o
16+
*.dyn_hi
17+
.hpc
18+
.hsenv
19+
.cabal-sandbox/
20+
cabal.sandbox.config
21+
*.prof
22+
*.aux
23+
*.hp
24+
*.eventlog
25+
.stack-work/
26+
cabal.project.local
27+
cabal.project.local~
28+
.HTF/
29+
.ghc.environment.*

.vscode/settings.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
"editor.defaultFormatter": "haskell.haskell",
1313
"editor.formatOnSave": true
1414
},
15-
"haskell.manageHLS": "GHCup",
15+
"haskell.manageHLS": "PATH", // nix
1616
"haskell.formattingProvider": "ormolu",
17-
"haskell.ghcupExecutablePath": "~/.ghcup/bin/ghcup",
17+
"haskell.ghcupExecutablePath": "",
18+
"haskell.upgradeGHCup": false,
19+
"haskell.serverExecutablePath": "haskell-language-server",
1820
"github.copilot.enable": {
1921
"haskell": false
2022
},
@@ -27,5 +29,5 @@
2729
"unlines",
2830
"zipWith",
2931
"elems"
30-
]
31-
}
32+
],
33+
}

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

math/newton/newtonRoot.hs renamed to WhyFunctionalProgrammingMatters/4_1newtonRoot.hs

File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
main :: IO ()
2+
main = do
3+
print $ (\input -> (+ 1) ((* 2) input)) 3 -- lambda式版
4+
print $ ((+ 1) . (* 2)) 3 -- 関数合成のほうが完結に書ける 3*2+1

0 commit comments

Comments
 (0)