Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

posts/2020/abstractions/ #1

Open
utterances-bot opened this issue May 5, 2020 · 3 comments
Open

posts/2020/abstractions/ #1

utterances-bot opened this issue May 5, 2020 · 3 comments

Comments

@utterances-bot
Copy link

The Beauty of Abstraction

Our world is modular, made up of complex things working independently yet together - all perfect examples of abstractions. It is hence important to appreciate its beauty.

https://mohitkarekar.com/posts/2020/abstractions/

Copy link

Beautiful article written about how concept of abstraction can be explained using simile. Just one more thing, I don't know why you would include unnecessary variables in factorial function because its as simple as-

def factorial(fact):
if(fact==1):
return 1
return fact*factorial(fact-1)

Maybe it was intended to be that way to express abstraction but all in all a good one.

Copy link
Owner

mohitk05 commented May 5, 2020

Hey, thanks for going through the post :) The factorial function in the post is a tail-recursive one. I've mentioned the difference in the block below.

The last line of the function you have written is return fact * factorial(fact - 1), so when recursion occurs, the compiler would have to return to this line to complete the function. This is because it would have to multiply the value of factorial(fact - 1) with fact and then return the value to the previous callee.

Instead, if you pass the value of factorial with each recursive call, and return only the factorial term, language compilers can perform certain optimisations in terms of skipping a reference to this call, as the function would have no more steps to perform.

You can read more here: https://www.geeksforgeeks.org/tail-recursion/

Copy link

True, it wouldn't return to the previous callee this way. Thank you for pointing it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants