Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Anonymous Recursion in C++
Search
na-o-ys
May 19, 2016
Programming
440
0
Share
Anonymous Recursion in C++
C++で無名再帰を行う
source code:
https://gist.github.com/na-o-ys/56434282baa5705efe7e3bd05e3f04d1
na-o-ys
May 19, 2016
More Decks by na-o-ys
See All by na-o-ys
IoTと監視
naoys
1
820
RubyとJIT
naoys
0
180
将棋盤を画像認識したかった
naoys
0
1.6k
Rust で乗り換え案内
naoys
0
650
疎行列と Jaccard 類似度の高速計算
naoys
1
670
有理数集合の濃度
naoys
2
150
YARVの最適化について調べた
naoys
0
160
転職会議サービスのAWS移行記録
naoys
0
88
入門AlphaGo
naoys
5
3.8k
Other Decks in Programming
See All in Programming
Symfony + NelmioApiDocBundle を使った スキーマ駆動開発 / Schema Driven Development with NelmioApiDocBundle
okashoi
0
270
Linux Kernelの1文字のミスで 権限昇格ができた話
rqda
0
2.3k
今年もTECHSCOREブログを書き続けます!
hiraoku101
0
230
AI時代のPhpStorm最新事情 #phpcon_odawara
yusuke
0
130
Swift Concurrency Type System
inamiy
0
290
AIと共にエンジニアとPMの “二刀流”を実現する
naruogram
0
130
Coding as Prompting Since 2025
ragingwind
0
760
Go_College_最終発表資料__外部公開用_.pdf
xe_pc23
0
130
年間50登壇、単著出版、雑誌寄稿、Podcast出演、YouTube、CM、カンファレンス主催……全部やってみたので面白さ等を比較してみよう / I’ve tried them all, so let’s compare how interesting they are.
nrslib
4
720
10年分の技術的負債、完済へ ― Claude Code主導のAI駆動開発でスポーツブルを丸ごとリプレイスした話
takuya_houshima
0
1.8k
ネイティブアプリとWebフロントエンドのAPI通信ラッパーにおける共通化の勘所
suguruooki
0
250
それはエンジニアリングの糧である:AI開発のためにAIのOSSを開発する現場より / It serves as fuel for engineering: insights from the field of developing open-source AI for AI development.
nrslib
1
830
Featured
See All Featured
Building Flexible Design Systems
yeseniaperezcruz
330
40k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
110
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
310
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
2
680
Tell your own story through comics
letsgokoyo
1
890
Designing for humans not robots
tammielis
254
26k
Being A Developer After 40
akosma
91
590k
Statistics for Hackers
jakevdp
799
230k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.1k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
110k
Transcript
Anonymous Recursion in C++ 0x64ޠ ୈ08 “Functional Programming” @na_o_ys
Anonymous Recursion (ແ໊࠶ؼ)
ී௨ͷ࠶ؼ (ϑΟϘφον) #include <iostream> int fib(int n) { return n
<= 1 ? 1 : fib(n-1) + fib(n-2); } int main(int argc, char const* argv[]) { std::cout << fib(4) << std::endl; return 0; }
Έ #include <iostream> int fib(int n) { return n <=
1 ? 1 : fib(n-1) + fib(n-2); } int main(int argc, char const* argv[]) { std::cout << fib(4) << std::endl; return 0; } ͜͜Ͱ͔͠Θͳ͍ͷʹ ͍͍ͪͪؔఆٛ͠ͳ͖Ό͍͚ͳ͍
ղܾҊ: ϥϜμؔΛଈ࣌ධՁ͢Δ
ϥϜμͰॻ͍ͯΈΔ #include <iostream> int main(int argc, char const* argv[]) {
std::cout << ([](int n) -> int { return n <= 1 ? 1 : fib(n-1) + fib(n-2); })(4) << std::endl; return 0; } ※ίϯύΠϧͰ͖·ͤΜ
#include <iostream> int main(int argc, char const* argv[]) {
std::cout << ([](int n) -> int { return n <= 1 ? 1 : fib(n-1) + fib(n-2); })(4) << std::endl; return 0; } ໊લ͕ແ͍ͷͰࣗࣗΛݺͳ͍ = ࠶ؼͰ͖ͳ͍ʂ
ແ໊࠶ؼͱ • ϥϜμؔʹ͓͍ͯ࠶ؼΛߦ͏͜ͱ • ෆಈίϯϏωʔλΛ͏ํ๏͕Ұൠత • 1. ҾʹࣗࣗΛड͚औΔϥϜμΛॻ͍ͯ • 2.
ෆಈίϯϏωʔλʹ৯ΘͤΔ
1. ҾʹࣗࣗΛड͚औΔ ϥϜμΛॻ͍ͯ [](auto self, int n) -> int {
return n <= 1 ? 1 : self(n-1) + self(n-2); } ࣗࣗ ※Ҿͷܕʹ auto Λࢦఆͨ͠ϥϜμɺςϯϓϨʔτؔΦϒδΣΫτͱͯ͠ ѻΘΕΔ(generic lambda: C++14 ͰՃ)
2. ෆಈίϯϏωʔλʹ৯ΘͤΔ // 不動点コンビネータを作る template<typename Func> struct fixed { Func
f; template<typename... Args> auto operator()(Args... args) { return f(fix(f), args...); } }; template<typename Func> fixed<Func> fix(Func f) { return fixed<Func> { f }; } ~~~~~~ // 食わせる fix( [](auto self, int n) -> int { return n <= 1 ? 1 : self(n-1) + self(n-2); } )
ಈ͘ʂ #include <iostream> // 省略 int main(int argc, char const*
argv[]) { std::cout << (fix( [](auto self, int n) -> int { return n <= 1 ? 1 : self(n-1) + self(n-2); } ))(4) << std::endl; return 0; } $ g++-6 -std=c++1y sample.cpp $ ./a.out > 5
GCD (࠷খެഒ) ͱ͔ • ࠶ؼͰॻ͚ҰߦͰࡁΉͭ #include <iostream> // 省略 int
main(int argc, char const* argv[]) { std::cout << (fix( [](auto self, int a, int b) -> int { return b == 0 ? a : self(b, a % b); } ))(15, 9) << std::endl; return 0; } $ g++-6 -std=c++1y sample.cpp $ ./a.out > 3
͓·͚: ෆಈίϯϏωʔλ
ෆಈίϯϏωʔλ • ఆٛ: fix(f)(args) = f(fix(f), args) • ্ࣜΛͣͬͱோΊͯΔͱɺಈ͘ཧ༝͕ݟ͑ͯ ͖·͢
• ώϯτ • ӈล f: ఆٛͨ͠ϥϜμࣜ • f(self, args) • ࠨล fix(f): argsͷΈΛड͚औΔؔ • = self template<typename Func> struct fixed { Func f; template<typename... Args> auto operator()(Args... args) { return f(fix(f), args...); } }; template<typename Func> fixed<Func> fix(Func f) { return fixed<Func> { f }; }
YίϯϏωʔλ? • ಈ͔͢͜ͱ͕తͳΒYίϯϏωʔλͱ͔Θͳ͍Ͱྑ͍ • ࠓճΈ͍ͨʹෆಈίϯϏωʔλͷ࠶ؼతͳఆٛΛͦͷ· ·ॻ͖Լͤྑ͍ • ྫ͑ Haskell ͷෆಈίϯϏωʔλͱͯγϯϓϧ:
fix f = f (fix f) • YίϯϏωʔλͱ͔ZίϯϏωʔλඇ࠶ؼతͳͷͰ͍͢͝ • ཧతʹॏཁɺΒ͍͠
ࢀߟ • Recursive Lambda functions in C++14 | Stack Overflow
http://stackoverflow.com/a/18085333