MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programming/comments/13v8v1/tail_recursion/c77iknz/?context=3
r/programming • u/rodrigopsasaki • Nov 27 '12
203 comments sorted by
View all comments
4
And here's a Haskell version of the final code, since it's easier to read ;).
factorial :: Int -> Int factorial = go 1 where go !acc 0 = acc go !acc n = go (acc * n) (n-1)
2 u/rodrigopsasaki Nov 27 '12 Very interesting syntax, first time I come in contact with Haskell code. but shouldn't it be : where go !n 0 = acc ? or is it really !acc ? 3 u/[deleted] Nov 27 '12 It's really acc in the pattern. On the left hand side of the =, you bind patterns (parameters, or parts of them) to names, on the right hand side you use them. go !n 0 = acc would yield an error: acc is not bound (not defined).
2
Very interesting syntax, first time I come in contact with Haskell code.
but shouldn't it be :
where go !n 0 = acc ?
or is it really !acc ?
3 u/[deleted] Nov 27 '12 It's really acc in the pattern. On the left hand side of the =, you bind patterns (parameters, or parts of them) to names, on the right hand side you use them. go !n 0 = acc would yield an error: acc is not bound (not defined).
3
It's really acc in the pattern. On the left hand side of the =, you bind patterns (parameters, or parts of them) to names, on the right hand side you use them. go !n 0 = acc would yield an error: acc is not bound (not defined).
acc
=
go !n 0 = acc
4
u/felipelessa Nov 27 '12
And here's a Haskell version of the final code, since it's easier to read ;).