<div dir="ltr">on Date: Fri, 27 Oct 2017 17:52:54 +0100 Johannes Weiß <<a href="mailto:johannesweiss@apple.com">johannesweiss@apple.com</a>> wrote:<div><br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
> On 27 Oct 2017, at 6:27 am, Howard Lovatt via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
><br>
> In terms of recursion you can fiddle it:<br>
><br>
> struct RecursiveClosure<C> {<br>
> var c: C! = nil<br>
> }<br>
> func factorial(_ n: Int) -> Int {<br>
> var recursive = RecursiveClosure<(Int) -> Int>()<br>
> recursive.c = { x in<br>
> (x == 0) ? 1 : x * recursive.c(x - 1)<br>
> }<br>
> return recursive.c(n)<br>
> }<br>
> factorial(5) // 120</blockquote><div><br></div><div>what a hack and a half :)</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">sorry, offtopic to the thread but that you can have easier with the fixed-point combinator (<a href="https://en.wikipedia.org/wiki/Fixed-point_combinator" rel="noreferrer" target="_blank">https://en.wikipedia.org/<wbr>wiki/Fixed-point_combinator</a>)<br></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
// the fixed-point combinator<br>
func fix<T>(_ f: @escaping ((@escaping (T) -> T) -> (T) -> T)) -> (T) -> T {<br>
return { (x: T) in (f(fix(f)))(x) }<br>
}<br>
<br>
// demo<br>
let fact = fix { fact_ in { n in n == 1 ? 1 : n * fact_(n-1) } }<br>
for i in 1..<10 {<br>
print(fact(i))<br>
}<br></blockquote><div><br></div><div>that would be a serious crime against humanity if swift allows this type of code at all :-)</div><div><br></div><div>Mike</div><div><br></div></div></div></div></div>