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
0
430
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
CSC307 Lecture 07
javiergs
PRO
0
550
Smart Handoff/Pickup ガイド - Claude Code セッション管理
yukiigarashi
0
130
AIと一緒にレガシーに向き合ってみた
nyafunta9858
0
230
生成AIを使ったコードレビューで定性的に品質カバー
chiilog
1
260
Unicodeどうしてる? PHPから見たUnicode対応と他言語での対応についてのお伺い
youkidearitai
PRO
1
2.5k
20260127_試行錯誤の結晶を1冊に。著者が解説 先輩データサイエンティストからの指南書 / author's_commentary_ds_instructions_guide
nash_efp
1
960
Oxlintはいいぞ
yug1224
5
1.3k
AI Agent Tool のためのバックエンドアーキテクチャを考える #encraft
izumin5210
6
1.8k
QAフローを最適化し、品質水準を満たしながらリリースまでの期間を最短化する #RSGT2026
shibayu36
2
4.4k
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
550
Grafana:建立系統全知視角的捷徑
blueswen
0
330
[KNOTS 2026登壇資料]AIで拡張‧交差する プロダクト開発のプロセス および携わるメンバーの役割
hisatake
0
280
Featured
See All Featured
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
120
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.2k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
1
51
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
1
1.4k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
57
50k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
170
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
55k
How to train your dragon (web standard)
notwaldorf
97
6.5k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
940
A Soul's Torment
seathinner
5
2.2k
So, you think you're a good person
axbom
PRO
2
1.9k
Mind Mapping
helmedeiros
PRO
0
81
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