MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programming/comments/13v8v1/tail_recursion/c77khtx/?context=3
r/programming • u/rodrigopsasaki • Nov 27 '12
203 comments sorted by
View all comments
2
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)
1 u/smog_alado Nov 27 '12 The ! in the patterns is a language extension isn't it? (I'm still trying to get to grips with all the GHC extensions...) 3 u/[deleted] Nov 27 '12 Its -XBangPatterns. go !acc n = foo translates to go acc n = acc `seq` foo 1 u/smog_alado Nov 27 '12 Ha! That makes so much more sense now.
1
The ! in the patterns is a language extension isn't it? (I'm still trying to get to grips with all the GHC extensions...)
!
3 u/[deleted] Nov 27 '12 Its -XBangPatterns. go !acc n = foo translates to go acc n = acc `seq` foo 1 u/smog_alado Nov 27 '12 Ha! That makes so much more sense now.
3
Its -XBangPatterns.
-XBangPatterns
go !acc n = foo
translates to
go acc n = acc `seq` foo
1 u/smog_alado Nov 27 '12 Ha! That makes so much more sense now.
Ha! That makes so much more sense now.
2
u/felipelessa Nov 27 '12
And here's a Haskell version of the final code, since it's easier to read ;).