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

Commit b054545

Browse files
feat: Add function composition example and configure VS Code Haskell settings for Nix HLS.
1 parent b7c89a0 commit b054545

8 files changed

Lines changed: 48 additions & 33 deletions

File tree

.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+
}

WhyFunctionalProgrammingMatters/3_tree.hs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,42 @@
11
-- https://www.sampou.org/haskell/article/whyfp.html より
2-
data Tree a = Node a [Tree a]
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)
36

47
-- 木のリストに対する畳み込み
58
redtree ::
6-
(a -> b -> b) -> -- f : node を潰す
7-
(b -> b -> b) -> -- g : cons を潰す
8-
b -> -- a : nil を潰す
9+
(a -> c -> b) -> -- f : node を潰す
10+
(b -> c -> c) -> -- g : cons を潰す
11+
c -> -- a : nil を潰す
912
Tree a ->
1013
b
1114
redtree f g a (Node label subtrees) =
1215
f label (redtree' f g a subtrees)
1316

1417
-- ツリーのリストを処理する関数
1518
redtree' ::
16-
(a -> b -> b) ->
17-
(b -> b -> b) ->
18-
b ->
19+
(a -> c -> b) ->
20+
(b -> c -> c) ->
21+
c ->
1922
[Tree a] ->
20-
b
23+
c
2124
redtree' f g a (subtree : rest) =
2225
g
2326
(redtree f g a subtree) -- リストのサイズが1に分解して潰す --> f label (redtree' f g a [先頭の木]) ...という流れでredtree' _ _ a [] = aにたどりつく
2427
(redtree' f g a rest) -- 残りで再帰
2528
redtree' _ _ a [] =
2629
a
2730

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+
2840
tree :: Tree Int
2941
tree =
3042
Node
@@ -40,15 +52,6 @@ tree =
4052
)
4153
)
4254

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-
5255
main :: IO ()
5356
main = do
5457
print $ sumtree tree -- 10

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
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# なぜ関数プログラミングは重要かのサンプルコード
2+
3+
## ABOUT
4+
5+
<https://www.sampou.org/haskell/article/whyfp.html>
6+
7+
上記論文に登場するサンプルコードをHaskellで実装したもの
8+
9+
---
10+
11+
## INDEX
12+
13+
- [3_list.hs](3_list.hs): 再帰によるリスト操作
14+
- [3_tree.hs](3_tree.hs): 再帰による木構造操作
15+
- [4_functionComposition.hs](4_functionComposition.hs): 関数合成
16+
- [4_1newtonRoot.hs](4_1newtonRoot.hs): ニュートンラフソン法
17+
- [4_2differential.hs](4_2differential.hs): 微分
18+
- [4_3integral.hs](4_3integral.hs): 積分

math/README.md

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

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

175
[euclidean](./euclidean/)

0 commit comments

Comments
 (0)