Haskell には手続き型言語で慣れ親しんだfor文が存在しない
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
printf("%d\n", sum);
このように1から10までを足す関数を書くときは
sum :: [Int] -> Int
sum [] = 0
sum (x:xs) = x + sum xs
main = print (sum [1..10])
再帰を使って書くことになる
- 再帰ドリル
- 末尾再帰最適化
- Spirals, Snowflakes & Trees: Recursion in Pictures
- Haskell Basics: How to Loop
- Haskell Diary - #1 Recursion
- Some simple derivations of recursive functions
- Polymorphic recursion combinator in Haskell
- Mutual Recursion in Final Encoding
- Intro to Recursion (Haskell)
- Mutual Recursion in Final Encoding
- Foldable.mapM_, Maybe, and recursive functions
- Follow up on mapM_ - Michael Snoyman’s blog
- haskell - Is there any way to inline a recursive function? - Stack Overflow
- 余再帰あれこれ
- goな関数 - あどけない話
- Travis Athougies - Dynamic Programming in Haskell is Just Recursion
- 不動点コンビネータで無名再帰を作る流れをおさらい - Qiita
- Haskell でループする - Qiita
- Rebecca Skinner - The Fixed Point
- [1608.05233] Coinductive Soundness of Corecursive Type Class Resolution
map, filter, foldr
変な話なのですが、再帰をよく理解したら、なるべく再帰を使ってはいけません
出典: Haskellの文法(再帰編)
高階関数で実現できる再帰のパターンは再帰で書かない方が読みやすい
doubleList :: [Int] -> [Int]
-- doubleList [] = []
-- doubleList (x:xs) = (x * 2) : doubleList xs
doubleList xs = map (*2) xs
evenElements :: [Int] -> [Int]
-- evenElements [] = []
-- evenElements (x:xs) = if even x then (x : evenElements xs) else evenElements xs
evenElements xs = filter even xs
foldl, foldr
- 正格データ(数値)を生成するには末尾再帰
- 遅延データ(リスト)を生成するには普通の再帰
出典: http://d.hatena.ne.jp/kazu-yamamoto/20091122/1258899591
- foldl
- Type introduction illustrated for casual haskellers
- Examining Hackage: folds
- A tutorial on the universality and expressiveness of fold
- foldlをfoldrで実装する
- The Magic of Folds
- effectfully/prefolds
- foldl vs foldl’
- foldl vs. foldl’に終止符を打つ - Qiita
- Fold and Unfold for Program Semantics
- aymannadeem/foldilocks: Tutorial using ghci to make folds easier. Come for the tutorial, stay for the fold puns.
- Rewriting functions with fold and reduce | Max Strübing
- Neil Mitchell’s Haskell Blog: foldr under the hood
- It never makes sense to use foldl on lists
- danso - fromMaybe is Just a fold
リスト
Foldable/Traversable
Foldable
class Foldable t where
foldr :: (a -> b -> b) -> b -> t a -> b
- Data.Foldable
- Foldable with metadata
- The reducers package
- Data.Foldableの正体 - Qiita
- パラメトリシティを使って自由モノイドが構成できることの証明 - Qiita
- たのしい Foldable - Qiita
- Data.Foldableの正体 - Qiita
- HaskellのFoldableはどこから来たのか? - Qiita
- What’s That Typeclass: Foldable
Traversable
class (Functor t, Foldable t) => Traversable t where
sequenceA :: Applicative f => t (f a) -> f (t a)
- Data.Traversable
- Traversable functor
- Domains, Sets, Traversals and Applicatives
- On the Static Nature of Traversals
- The Essence of the Iterator Pattern
- witherable
- snoyberg/mono-traversable
- Proposal: Suggest explicit type application for Foldable length and friends : Inside 214-1E
- Accidentally Quadratic — GHC Derived Foldable and Traversable Instances
- Traversable: A Remix - The Life Monadic
- benjamin.pizza - Zip-Folding
- Polymorphic
mono-traversable
- Breadth-First Traversals in Far Too Much Detail - Donnacha Oisín Kidney
- Implicit Corecursive Queues - Donnacha Oisín Kidney
- Applicative Sorting
- treeowl/sort-traversable - Sort any Traversable container
- Icicle Lang - Traversals as Optimisations
- Traversable のための圏論
- 関数型言語におけるTraversableとは何か?Functorと比較しながら解説する - Qiita
- Too lazy to evaluate - FilterableとWitherableについて
Distributive
Distributive is the categorical dual of Traversable.
class Functor g => Distributive g where
distribute :: Functor f => f (g a) -> g (f a)
Recursion Schemes
- Recursion-scheme-generator
- recursion-schemes: Representing common recursion patterns as higher-order functions
- n番煎じのrecursion-scheme - The curse of λ
- CATEGORICAL PROGRAMMING WITH INDUCTIVE AND COINDUCTIVE TYPES
- youzicha — Generalized Church is the Curry-Howard of…
F始代数
- F始代数
- 代数的データ型とF代数 - Qiita
- Understanding F-Algebras
- Catamorphisms in 15 Minutes!
- Finite trees as initial algebra
- Fixed points of functors
- A Study of Categories of Algebras and Coalgebras
- Fundamental Study Universal coalgebra: a theory of systems
- Coalgebras in functional programming and type theory
cata, ana, hylo, para
- Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire
- Review: Bananas, Lenses, Envelopes and Barbed Wire :: Reasonably Polymorphic
- 再帰のパターン
- Practical Recursion Schemes
- What’s in a Fold: The Basic Catamorphism in recursion-schemes - The Life Monadic
- The Monad Cat - Programming with bananas and barbed wire. Part 1
- Practical Recursion Schemes · jtobin.io
- A Tour of Some Useful Recursive Types · jtobin.io
- Monadic Recursion Schemes · jtobin.io
- Stalking a Hylomorphism in the Wild | Bartosz Milewski’s Programming Cafe
- Don’t fear the cat-amorphism (nor the hylomorphism)
- Recursion Schemes By Example
- Sorting with Style · jtobin.io - hyloを使ってmergeソートを実装する
- Sorting Slower with Style · jtobin.io - apoを使って挿入ソートを実装する
- おじいさん、今日のご飯はCatamorphismですよ - Qiita
- おじいさん、今日のご飯はAnamorphismですよ - Qiita
- おじいさん、今日のご飯はHylomorphismですよ - Qiita
- Terminal Coalgebra as Directed Limit | Bartosz Milewski’s Programming Cafe
- Monoidal Catamorphism
- ASTs with Fix and Free
- Combining folds using semigroups
- Anamorphisms aka Unfolds Explained | Functional Works
newtype Fix f = In { out :: f (Fix f) }
type Algebra f a = f a -> a
-- Church encoding
type Fix f = forall a. Algebra f a -> a
- Functorial polymorphism - ScienceDirect
- https://www.cs.utexas.edu/~wcook/Drafts/2012/MTC.pdf
- Haskell for all: The visitor pattern is essentially the same thing as Church encoding
mutu
- (PDF) Make it Practical: A Generic Linear-Time Algorithm for Solving Maximum-Weightsum Problems
- 最大重み和問題の線形時間アルゴリズムの導出
- ナップサック問題およびその発展問題の統一的解法
融合変換
Other morphisms
- Zygomorphisms and Futumorphisms specialised to lists
- Time Traveling Recursion Schemes · jtobin.io
- Promorphisms, Pre and Post
- somehow-morphisms on fixed point written in Haskell
- The Comonad.Reader » Unnatural Transformations
- Recursion Schemes for Higher Algebras | Bartosz Milewski’s Programming Cafe
- Generic Programming with Adjunctions
動的計画法
- Haskell で動的計画法を書くための3つの方針
- 「組合せ最適化でグループ分け」Haskellでやってみた
- Dynamorphism 〜 Haskellでも動的計画法がしたい! 〜
- 動的計画法にData.Vector.constructNは使うべきではない。 - Qiita
- The Comonad.Reader » Dynamorphisms as Chronomorphisms
応用例
- Program Reduction: A Win for Recursion Schemes
- A Simple Hylomorphism Example – Colour Coding
- A catamorphic lambda-calculus interpreter
- An Introduction to Recursion Schemes
- Recursion Schemes, Part II: A Mob of Morphisms
- Recursion Schemes, Part III: Folds in Context
- Recursion Schemes, Part IV: Time is of the Essence
- Recursion Schemes, Part 4½: Better Living Through Base Functors
- Recursion Schemes, Part V: Hello, Hylomorphisms
- Recursion Schemes, Part VI: Comonads, Composition, and Generality
- Haskell at Barclays: Exotic tools for exotic trades
- Compdata Trees and Catamorphisms
- 累積百ます計算,パスカルの三角形,関・ベルヌーイ数を計算する - Qiita