SlideShare a Scribd company logo
{
func fac(_ n: Int) -> Int {
guard n > 0 else { return 1 }
return n * fac(n-1)
}
fac(4)
➔ 4 * fac(3)
➔ 4 * 3 * fac(2)
➔ 4 * 3 * 2 * fac(1)
➔ 4 * 3 * 2 * 1 * fac(0)
➔ 4 * 3 * 2 * 1 * 1
➔ 4 * 3 * 2 * 1
➔ 4 * 3 * 2
➔ 4 * 6
➔ 24
func fac(_ n: Int) -> Int {
func fac(_ index: Int, _ answer: Int) -> Int {
if index == 1 {
return answer
}
return fac(index-1, answer * index)
}
return fac(n,1)
}
fac(4)
➔ fac(4,1)
➔ fac(3,4)
➔ fac(2,12)
➔ fac(1,24)
➔ 24
.
{
func fib(_ n: Int) -> Int {
if n == 0 { return 0 }
if n == 1 { return 1 }
return fib(n-1) + fib(n-2)
}
fib(5)
fib(4) fib(3)
fib(1)
1
fib(2)
fib(0)
0
fib(1)
1
fib(2)
fib(0)
0
fib(1)
1
fib(3)
fib(1)
1
fib(2)
fib(0)
0
fib(1)
1
func fib(_ n: Int) -> Int {
func fib(_ a: Int, _ b: Int, _ n: Int) -> Int {
if n == 0 { return a }
return fib(b, a+b, n-1)
}
return fib(0,1,n)
}
fib(6)
➔ fib(0,1,6)
➔ fib(1,1,5)
➔ fib(1,2,4)
➔ fib(2,3,3)
➔ fib(3,5,2)
➔ fib(5,8,1)
➔ fib(8,13,0)
➔ 8
fac(4)
➔ fac(4,1)
➔ fac(3,4)
➔ fac(2,12)
➔ fac(1,24)
➔ 24
fac(4)
➔ 4 * fac(3)
➔ 4 * 3 * fac(2)
➔ 4 * 3 * 2 * fac(1)
➔ 4 * 3 * 2 * 1 * fac(0)
➔ 4 * 3 * 2 * 1 * 1
➔ 4 * 3 * 2 * 1
➔ 4 * 3 * 2
➔ 4 * 6
➔ 24
: https://brunch.co.kr/@sunghokimnxag/5
8
• 1 .
• index 7 (7,14,21…) 7 (7,17,27) .
• 1, 2, 3, 4, 5, 6, [7], 6, 5, 4, 3, 2, 1, [0], 1, 2, [3], 2, 1, 0, [-1], 0, 1,…
• pingpong(x) .
• For Loop Array .
• Assignment , .
• String .
pingpong(8) = 6
pingpong(22) = 0
pingpong(68) = 2
pingpong(100) = 2
func has7(_ index: Int) -> Bool {
if index % 7 == 0 {
return true
}
if (index - 7) % 10 == 0 {
return true
}
if (index / 10) > 1 {
return has7(index/10)
}
return false
}
has7(21)
➔ true
has7(723)
➔ has7(72)
➔ has7(7)
➔ true
has7(123)
➔ has7(12)
➔ has7(1)
➔ false
unc pingpong(_ index: Int) -> Int {
func pingpong(_ index: Int) -> (Int, Int) {
if index == 1 {
return (1, 1)
}
switch pingpong( index - 1 ) {
case let (answer, direction) :
if has7(index) {
return (answer+direction, -direction)
} else {
return (answer+direction, direction)
}
}
}
return pingpong(index).0
pingpong(14) ➔ (pingpong(13).answer-1, 1)
pingpong(13) ➔ (pingpong(12).answer-1, -1)
pingpong(12) ➔ (pingpong(11).answer-1, -1)
pingpong(11) ➔ (pingpong(10).answer-1, -1)
pingpong(10) ➔ (pingpong( 9).answer-1, -1)
pingpong(9) ➔ (pingpong( 8).answer-1, -1)
pingpong(8) ➔ (pingpong( 7).answer-1, -1)
pingpong(7) ➔ (pingpong( 6).answer+1, -1)
pingpong(6) ➔ (pingpong( 5).answer+1, 1)
pingpong(5) ➔ (pingpong( 4).answer+1, 1)
pingpong(4) ➔ (pingpong( 3).answer+1, 1)
pingpong(3) ➔ (pingpong( 2).answer+1, 1)
pingpong(2) ➔ (pingpong( 1).answer+1, 1)
pingpong(1) ➔ (1,1)
nc pingpong(_ count: Int) -> Int {
func pp(_ count: Int, _ index: Int, _ direction: Int, _ answer: Int) -> Int {
if count == index {
return answer
}
if has7(index+1) {
return pp(count, index+1, -direction, answer+direction)
} else {
return pp(count, index+1, direction, answer+direction)
}
}
return pp(count, 1, 1, 1)
pp(14, index: 1, direction: 1,answer: 1)
pp(14, index: 2, direction: 1,answer: 2)
pp(14, index: 3, direction: 1,answer: 3)
pp(14, index: 4, direction: 1,answer: 4)
pp(14, index: 5, direction: 1,answer: 5)
pp(14, index: 6, direction:-1,answer: 6)
pp(14, index: 7, direction:-1,answer: 7)
pp(14, index: 8, direction:-1,answer: 6)
pp(14, index: 9, direction:-1,answer: 5)
pp(14, index:10, direction:-1,answer: 4)
pp(14, index:11, direction:-1,answer: 3)
pp(14, index:12, direction:-1,answer: 2)
pp(14, index:13, direction: 1,answer: 1)
➔ 0
let array = [1,3,5,23,15]
func sum(array: [Int], initial: Int) -> Int {
guard let head = array.first else { return initial }
let tail = Array(array.dropFirst())
return sum(array: tail, initial: initial + head)
}
sum(array: array, initial: 0)
➔ sum(array: [1,3,5,23,15], initial: 0)
➔ sum(array: [3,5,23,15], initial: 1)
➔ sum(array: [5,23,15], initial: 4)
➔ sum(array: [23,15], initial: 9)
➔ sum(array: [15], initial: 32)
➔ sum(array: [], initial: 47)
➔ 47
func tailRecursive(array: [Int] ,
initial: Int,
eachOperation: (Int, Int) -> Int ) -> Int {
guard let head = array.first else { return initial }
let tail = Array(array.dropFirst())
return tailRecursive(array: tail,
initial: eachOperation(head, initial),
eachOperation: eachOperation)
}
tailRecursive(array: array,
initial: 0,
eachOperation:{(element: Int, answer: Int)->Int in
element + answer
})
➔ 47
extension Array {
func tailRecursive<Result>(
_ initial: Result,
eachOperation: (Result, Element) -> Result ) -> Result {
guard let head = self.first else { return initial }
let tail = Array(self.dropFirst())
return tail.tailRecursive(eachOperation(initial, head),
eachOperation: eachOperation)
}
}
array.tailRecursive(0, eachOperation: + )
array.tailRecursive(0) { (element: Int, answer: Int) -> Int in
return element + answer
}
array.reduce(0) { (element: Int, answer) -> Int in
return element + answer
}
Array(1...5).reduce(1, *)
• .
• .
-> .
-> ex)
-> .
func searchTopViewController(viewController: UIViewController?) -> UIViewController? {
guard let viewController = viewController else { return nil }
switch viewController {
case let viewController as UITabBarController:
return searchTopViewController(viewController: viewController.selectedViewController)
case let viewController as UINavigationController:
return searchTopViewController(viewController: viewController.viewControllers.last)
case let viewController where viewController.presentedViewController != nil:
return searchTopViewController(viewController:viewController.presentedViewController)
default:
return viewController
}
}
•
•
•
• Reduce
•

More Related Content

What's hot

Pooya Khaloo Presentation on IWMC 2015
Pooya Khaloo Presentation on IWMC 2015Pooya Khaloo Presentation on IWMC 2015
Pooya Khaloo Presentation on IWMC 2015
Iran Entrepreneurship Association
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
UC San Diego
 
Groovy vs Boilerplate and Ceremony Code
Groovy vs Boilerplate and Ceremony CodeGroovy vs Boilerplate and Ceremony Code
Groovy vs Boilerplate and Ceremony Code
stasimus
 
Synapse india complain sharing info about php chaptr 26
Synapse india complain sharing info about php chaptr 26Synapse india complain sharing info about php chaptr 26
Synapse india complain sharing info about php chaptr 26
SynapseindiaComplaints
 
Ciklum net sat12112011-alexander fomin-expressions and all, all, all
Ciklum net sat12112011-alexander fomin-expressions and all, all, allCiklum net sat12112011-alexander fomin-expressions and all, all, all
Ciklum net sat12112011-alexander fomin-expressions and all, all, all
Ciklum Ukraine
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
riue
 
Codeware
CodewareCodeware
Codeware
Uri Nativ
 
RxSwift 활용하기 - Let'Swift 2017
RxSwift 활용하기 - Let'Swift 2017RxSwift 활용하기 - Let'Swift 2017
RxSwift 활용하기 - Let'Swift 2017
Wanbok Choi
 
c programming
c programmingc programming
c programming
Arun Umrao
 
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf MilanFrom Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
Fabio Collini
 
Intro programacion funcional
Intro programacion funcionalIntro programacion funcional
Intro programacion funcional
NSCoder Mexico
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
ssuserd6b1fd
 
Scala best practices
Scala best practicesScala best practices
Scala best practices
Alexander Zaidel
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
ssuserd6b1fd
 
Unit 3
Unit 3 Unit 3
Unit 3
GOWSIKRAJAP
 
20180721 code defragment
20180721 code defragment20180721 code defragment
20180721 code defragment
Chiwon Song
 
Slaying the Dragon: Implementing a Programming Language in Ruby
Slaying the Dragon: Implementing a Programming Language in RubySlaying the Dragon: Implementing a Programming Language in Ruby
Slaying the Dragon: Implementing a Programming Language in Ruby
Jason Yeo Jie Shun
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
Simon Proctor
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
Simon Proctor
 
20181020 advanced higher-order function
20181020 advanced higher-order function20181020 advanced higher-order function
20181020 advanced higher-order function
Chiwon Song
 

What's hot (20)

Pooya Khaloo Presentation on IWMC 2015
Pooya Khaloo Presentation on IWMC 2015Pooya Khaloo Presentation on IWMC 2015
Pooya Khaloo Presentation on IWMC 2015
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
Groovy vs Boilerplate and Ceremony Code
Groovy vs Boilerplate and Ceremony CodeGroovy vs Boilerplate and Ceremony Code
Groovy vs Boilerplate and Ceremony Code
 
Synapse india complain sharing info about php chaptr 26
Synapse india complain sharing info about php chaptr 26Synapse india complain sharing info about php chaptr 26
Synapse india complain sharing info about php chaptr 26
 
Ciklum net sat12112011-alexander fomin-expressions and all, all, all
Ciklum net sat12112011-alexander fomin-expressions and all, all, allCiklum net sat12112011-alexander fomin-expressions and all, all, all
Ciklum net sat12112011-alexander fomin-expressions and all, all, all
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
 
Codeware
CodewareCodeware
Codeware
 
RxSwift 활용하기 - Let'Swift 2017
RxSwift 활용하기 - Let'Swift 2017RxSwift 활용하기 - Let'Swift 2017
RxSwift 활용하기 - Let'Swift 2017
 
c programming
c programmingc programming
c programming
 
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf MilanFrom Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
 
Intro programacion funcional
Intro programacion funcionalIntro programacion funcional
Intro programacion funcional
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
 
Scala best practices
Scala best practicesScala best practices
Scala best practices
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
Unit 3
Unit 3 Unit 3
Unit 3
 
20180721 code defragment
20180721 code defragment20180721 code defragment
20180721 code defragment
 
Slaying the Dragon: Implementing a Programming Language in Ruby
Slaying the Dragon: Implementing a Programming Language in RubySlaying the Dragon: Implementing a Programming Language in Ruby
Slaying the Dragon: Implementing a Programming Language in Ruby
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
 
20181020 advanced higher-order function
20181020 advanced higher-order function20181020 advanced higher-order function
20181020 advanced higher-order function
 

Similar to Swift에서 꼬리재귀 사용기 (Tail Recursion)

Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
ikdysfm
 
Matematika diskrit: fungsi pembangkit part 3
Matematika diskrit: fungsi pembangkit part 3Matematika diskrit: fungsi pembangkit part 3
Matematika diskrit: fungsi pembangkit part 3
radar radius
 
Useful javascript
Useful javascriptUseful javascript
Useful javascript
Lei Kang
 
FUNCTIONS L.1.pdf
FUNCTIONS L.1.pdfFUNCTIONS L.1.pdf
FUNCTIONS L.1.pdf
Marjorie Malveda
 
Declare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term RewritingDeclare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term Rewriting
Eelco Visser
 
From Javascript To Haskell
From Javascript To HaskellFrom Javascript To Haskell
From Javascript To Haskell
ujihisa
 
Introduction to Recursion (Python)
Introduction to Recursion (Python)Introduction to Recursion (Python)
Introduction to Recursion (Python)
Thai Pangsakulyanont
 
Swift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdfSwift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdf
JkPoppy
 
Unit 1 Set Theory-Engineering Mathematics.pptx
Unit 1 Set Theory-Engineering Mathematics.pptxUnit 1 Set Theory-Engineering Mathematics.pptx
Unit 1 Set Theory-Engineering Mathematics.pptx
G2018ChoudhariNikita
 
A/B Testing for Game Design
A/B Testing for Game DesignA/B Testing for Game Design
A/B Testing for Game Design
Trieu Nguyen
 
Scala kansai summit-2016
Scala kansai summit-2016Scala kansai summit-2016
Scala kansai summit-2016
Naoki Kitora
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
Avjinder (Avi) Kaler
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
Avjinder (Avi) Kaler
 
cover every basics of python with this..
cover every basics of python with this..cover every basics of python with this..
cover every basics of python with this..
karkimanish411
 
Computation of Semi-Magic Squares Generated by Serpentine Matrices
Computation of Semi-Magic Squares Generated by Serpentine MatricesComputation of Semi-Magic Squares Generated by Serpentine Matrices
Computation of Semi-Magic Squares Generated by Serpentine Matrices
Lossian Barbosa Bacelar Miranda
 
Eta
EtaEta
funwithalgorithms.pptx
funwithalgorithms.pptxfunwithalgorithms.pptx
funwithalgorithms.pptx
Tess Ferrandez
 
Week-5-Inverse-Function-NGY.pptx
Week-5-Inverse-Function-NGY.pptxWeek-5-Inverse-Function-NGY.pptx
Week-5-Inverse-Function-NGY.pptx
ExtremelyDarkness2
 
Perl6 one-liners
Perl6 one-linersPerl6 one-liners
Perl6 one-liners
Andrew Shitov
 
Let’s Talk About Ruby
Let’s Talk About RubyLet’s Talk About Ruby
Let’s Talk About Ruby
Ian Bishop
 

Similar to Swift에서 꼬리재귀 사용기 (Tail Recursion) (20)

Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
Matematika diskrit: fungsi pembangkit part 3
Matematika diskrit: fungsi pembangkit part 3Matematika diskrit: fungsi pembangkit part 3
Matematika diskrit: fungsi pembangkit part 3
 
Useful javascript
Useful javascriptUseful javascript
Useful javascript
 
FUNCTIONS L.1.pdf
FUNCTIONS L.1.pdfFUNCTIONS L.1.pdf
FUNCTIONS L.1.pdf
 
Declare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term RewritingDeclare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term Rewriting
 
From Javascript To Haskell
From Javascript To HaskellFrom Javascript To Haskell
From Javascript To Haskell
 
Introduction to Recursion (Python)
Introduction to Recursion (Python)Introduction to Recursion (Python)
Introduction to Recursion (Python)
 
Swift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdfSwift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdf
 
Unit 1 Set Theory-Engineering Mathematics.pptx
Unit 1 Set Theory-Engineering Mathematics.pptxUnit 1 Set Theory-Engineering Mathematics.pptx
Unit 1 Set Theory-Engineering Mathematics.pptx
 
A/B Testing for Game Design
A/B Testing for Game DesignA/B Testing for Game Design
A/B Testing for Game Design
 
Scala kansai summit-2016
Scala kansai summit-2016Scala kansai summit-2016
Scala kansai summit-2016
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
 
cover every basics of python with this..
cover every basics of python with this..cover every basics of python with this..
cover every basics of python with this..
 
Computation of Semi-Magic Squares Generated by Serpentine Matrices
Computation of Semi-Magic Squares Generated by Serpentine MatricesComputation of Semi-Magic Squares Generated by Serpentine Matrices
Computation of Semi-Magic Squares Generated by Serpentine Matrices
 
Eta
EtaEta
Eta
 
funwithalgorithms.pptx
funwithalgorithms.pptxfunwithalgorithms.pptx
funwithalgorithms.pptx
 
Week-5-Inverse-Function-NGY.pptx
Week-5-Inverse-Function-NGY.pptxWeek-5-Inverse-Function-NGY.pptx
Week-5-Inverse-Function-NGY.pptx
 
Perl6 one-liners
Perl6 one-linersPerl6 one-liners
Perl6 one-liners
 
Let’s Talk About Ruby
Let’s Talk About RubyLet’s Talk About Ruby
Let’s Talk About Ruby
 

Recently uploaded

Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
KrishnaveniKrishnara1
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEMTIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
HODECEDSIET
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball playEric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
enizeyimana36
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
mamamaam477
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
NazakatAliKhoso2
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
mahammadsalmanmech
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
sachin chaurasia
 

Recently uploaded (20)

Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEMTIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball playEric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
 

Swift에서 꼬리재귀 사용기 (Tail Recursion)

  • 1.
  • 2.
  • 3. {
  • 4. func fac(_ n: Int) -> Int { guard n > 0 else { return 1 } return n * fac(n-1) } fac(4) ➔ 4 * fac(3) ➔ 4 * 3 * fac(2) ➔ 4 * 3 * 2 * fac(1) ➔ 4 * 3 * 2 * 1 * fac(0) ➔ 4 * 3 * 2 * 1 * 1 ➔ 4 * 3 * 2 * 1 ➔ 4 * 3 * 2 ➔ 4 * 6 ➔ 24
  • 5. func fac(_ n: Int) -> Int { func fac(_ index: Int, _ answer: Int) -> Int { if index == 1 { return answer } return fac(index-1, answer * index) } return fac(n,1) } fac(4) ➔ fac(4,1) ➔ fac(3,4) ➔ fac(2,12) ➔ fac(1,24) ➔ 24 .
  • 6. {
  • 7. func fib(_ n: Int) -> Int { if n == 0 { return 0 } if n == 1 { return 1 } return fib(n-1) + fib(n-2) } fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) 0 fib(1) 1 fib(2) fib(0) 0 fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) 0 fib(1) 1
  • 8. func fib(_ n: Int) -> Int { func fib(_ a: Int, _ b: Int, _ n: Int) -> Int { if n == 0 { return a } return fib(b, a+b, n-1) } return fib(0,1,n) } fib(6) ➔ fib(0,1,6) ➔ fib(1,1,5) ➔ fib(1,2,4) ➔ fib(2,3,3) ➔ fib(3,5,2) ➔ fib(5,8,1) ➔ fib(8,13,0) ➔ 8
  • 9. fac(4) ➔ fac(4,1) ➔ fac(3,4) ➔ fac(2,12) ➔ fac(1,24) ➔ 24 fac(4) ➔ 4 * fac(3) ➔ 4 * 3 * fac(2) ➔ 4 * 3 * 2 * fac(1) ➔ 4 * 3 * 2 * 1 * fac(0) ➔ 4 * 3 * 2 * 1 * 1 ➔ 4 * 3 * 2 * 1 ➔ 4 * 3 * 2 ➔ 4 * 6 ➔ 24
  • 10.
  • 11. : https://brunch.co.kr/@sunghokimnxag/5 8 • 1 . • index 7 (7,14,21…) 7 (7,17,27) . • 1, 2, 3, 4, 5, 6, [7], 6, 5, 4, 3, 2, 1, [0], 1, 2, [3], 2, 1, 0, [-1], 0, 1,… • pingpong(x) . • For Loop Array . • Assignment , . • String . pingpong(8) = 6 pingpong(22) = 0 pingpong(68) = 2 pingpong(100) = 2
  • 12. func has7(_ index: Int) -> Bool { if index % 7 == 0 { return true } if (index - 7) % 10 == 0 { return true } if (index / 10) > 1 { return has7(index/10) } return false } has7(21) ➔ true has7(723) ➔ has7(72) ➔ has7(7) ➔ true has7(123) ➔ has7(12) ➔ has7(1) ➔ false
  • 13. unc pingpong(_ index: Int) -> Int { func pingpong(_ index: Int) -> (Int, Int) { if index == 1 { return (1, 1) } switch pingpong( index - 1 ) { case let (answer, direction) : if has7(index) { return (answer+direction, -direction) } else { return (answer+direction, direction) } } } return pingpong(index).0 pingpong(14) ➔ (pingpong(13).answer-1, 1) pingpong(13) ➔ (pingpong(12).answer-1, -1) pingpong(12) ➔ (pingpong(11).answer-1, -1) pingpong(11) ➔ (pingpong(10).answer-1, -1) pingpong(10) ➔ (pingpong( 9).answer-1, -1) pingpong(9) ➔ (pingpong( 8).answer-1, -1) pingpong(8) ➔ (pingpong( 7).answer-1, -1) pingpong(7) ➔ (pingpong( 6).answer+1, -1) pingpong(6) ➔ (pingpong( 5).answer+1, 1) pingpong(5) ➔ (pingpong( 4).answer+1, 1) pingpong(4) ➔ (pingpong( 3).answer+1, 1) pingpong(3) ➔ (pingpong( 2).answer+1, 1) pingpong(2) ➔ (pingpong( 1).answer+1, 1) pingpong(1) ➔ (1,1)
  • 14. nc pingpong(_ count: Int) -> Int { func pp(_ count: Int, _ index: Int, _ direction: Int, _ answer: Int) -> Int { if count == index { return answer } if has7(index+1) { return pp(count, index+1, -direction, answer+direction) } else { return pp(count, index+1, direction, answer+direction) } } return pp(count, 1, 1, 1) pp(14, index: 1, direction: 1,answer: 1) pp(14, index: 2, direction: 1,answer: 2) pp(14, index: 3, direction: 1,answer: 3) pp(14, index: 4, direction: 1,answer: 4) pp(14, index: 5, direction: 1,answer: 5) pp(14, index: 6, direction:-1,answer: 6) pp(14, index: 7, direction:-1,answer: 7) pp(14, index: 8, direction:-1,answer: 6) pp(14, index: 9, direction:-1,answer: 5) pp(14, index:10, direction:-1,answer: 4) pp(14, index:11, direction:-1,answer: 3) pp(14, index:12, direction:-1,answer: 2) pp(14, index:13, direction: 1,answer: 1) ➔ 0
  • 15. let array = [1,3,5,23,15] func sum(array: [Int], initial: Int) -> Int { guard let head = array.first else { return initial } let tail = Array(array.dropFirst()) return sum(array: tail, initial: initial + head) } sum(array: array, initial: 0) ➔ sum(array: [1,3,5,23,15], initial: 0) ➔ sum(array: [3,5,23,15], initial: 1) ➔ sum(array: [5,23,15], initial: 4) ➔ sum(array: [23,15], initial: 9) ➔ sum(array: [15], initial: 32) ➔ sum(array: [], initial: 47) ➔ 47
  • 16. func tailRecursive(array: [Int] , initial: Int, eachOperation: (Int, Int) -> Int ) -> Int { guard let head = array.first else { return initial } let tail = Array(array.dropFirst()) return tailRecursive(array: tail, initial: eachOperation(head, initial), eachOperation: eachOperation) } tailRecursive(array: array, initial: 0, eachOperation:{(element: Int, answer: Int)->Int in element + answer }) ➔ 47
  • 17. extension Array { func tailRecursive<Result>( _ initial: Result, eachOperation: (Result, Element) -> Result ) -> Result { guard let head = self.first else { return initial } let tail = Array(self.dropFirst()) return tail.tailRecursive(eachOperation(initial, head), eachOperation: eachOperation) } } array.tailRecursive(0, eachOperation: + ) array.tailRecursive(0) { (element: Int, answer: Int) -> Int in return element + answer } array.reduce(0) { (element: Int, answer) -> Int in return element + answer }
  • 19. • . • . -> . -> ex) -> .
  • 20. func searchTopViewController(viewController: UIViewController?) -> UIViewController? { guard let viewController = viewController else { return nil } switch viewController { case let viewController as UITabBarController: return searchTopViewController(viewController: viewController.selectedViewController) case let viewController as UINavigationController: return searchTopViewController(viewController: viewController.viewControllers.last) case let viewController where viewController.presentedViewController != nil: return searchTopViewController(viewController:viewController.presentedViewController) default: return viewController } }