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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
na-o-ys
May 19, 2016
Programming
0
440
Anonymous Recursion in C++
C++で無名再帰を行う
source code:
https://gist.github.com/na-o-ys/56434282baa5705efe7e3bd05e3f04d1
na-o-ys
May 19, 2016
Tweet
Share
More Decks by na-o-ys
See All by na-o-ys
IoTと監視
naoys
1
810
RubyとJIT
naoys
0
170
将棋盤を画像認識したかった
naoys
0
1.6k
Rust で乗り換え案内
naoys
0
640
疎行列と Jaccard 類似度の高速計算
naoys
1
660
有理数集合の濃度
naoys
2
140
YARVの最適化について調べた
naoys
0
150
転職会議サービスのAWS移行記録
naoys
0
83
入門AlphaGo
naoys
5
3.8k
Other Decks in Programming
See All in Programming
dchart: charts from deck markup
ajstarks
3
990
[KNOTS 2026登壇資料]AIで拡張‧交差する プロダクト開発のプロセス および携わるメンバーの役割
hisatake
0
280
Honoを使ったリモートMCPサーバでAIツールとの連携を加速させる!
tosuri13
1
180
Smart Handoff/Pickup ガイド - Claude Code セッション管理
yukiigarashi
0
140
Unicodeどうしてる? PHPから見たUnicode対応と他言語での対応についてのお伺い
youkidearitai
PRO
1
2.5k
Apache Iceberg V3 and migration to V3
tomtanaka
0
160
OSSとなったswift-buildで Xcodeのビルドを差し替えられるため 自分でXcodeを直せる時代になっている ダイアモンド問題編
yimajo
3
620
【卒業研究】会話ログ分析によるユーザーごとの関心に応じた話題提案手法
momok47
0
200
16年目のピクシブ百科事典を支える最新の技術基盤 / The Modern Tech Stack Powering Pixiv Encyclopedia in its 16th Year
ahuglajbclajep
5
1k
Package Management Learnings from Homebrew
mikemcquaid
0
220
今から始めるClaude Code超入門
448jp
8
8.8k
Architectural Extensions
denyspoltorak
0
290
Featured
See All Featured
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.6k
GitHub's CSS Performance
jonrohan
1032
470k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.7k
Speed Design
sergeychernyshev
33
1.5k
Balancing Empowerment & Direction
lara
5
890
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
730
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
280
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
1.9k
How to Think Like a Performance Engineer
csswizardry
28
2.4k
Designing for Timeless Needs
cassininazir
0
130
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
110
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