+ 2
Explain this function please?
def cons(a,b): def pair(f): return f(a,b) return pair
16 Answers
+ 3
cons(a, b) doesn't return a pair, it returns the function 'pair' that is defined inside.
That returned pair seems to 'remember' the values a and b that were passed to cons ... which is confusing to me. Guess I have to repeat closures for myself.
Anyway, you could for example call it like this:
cons(3, 4)(max) # -> 4
cons(5, 2)(min) # -> 2
+ 6
Where have you found that one?
+ 5
Abhinav Verma Your perceived confidence in your earlier response caused so much confusion with your statement "There is nothing to it."
It gave the impression you _thought_ you knew the answer and decided to reveal it after no one got it right.
The answer presented as, "The given function/algorithm to be implemented is just to construct a pair" was incorrect.
You left it at that. No follow up clarifying you still had any doubt what the parameter/argument "f" was doing.
Nor did you acknowledge any of the previous comments that many of us provided in this thread providing details about python closures or our request for additional context.
You only clarified you were still confused after I further asked if this was a challenge.
This has been one of the most bizarre threads I've contributed to. My recommendation is, in the future, provide more clarification as stated earlier and acknowledge that you've reviewed the comments by others.
+ 5
Abhinav Verma If you still need more clarification beyond what HonFu clarified, the answers I posted provide additional details about the assumed usage of this python closure.
+ 5
Abhinav Verma Thanks for your previous response.
Ultimately, the many talented volunteers here share the same passion for helping others grow as developers. Part of that involves sharing our knowledge and another part involves providing guidance that will help members get the most out of this amazing community.
I have no doubt that as you continue to gain more experience and confidence with what you learn, you'll find yourself becoming a mentor to others here as well.
Welcome to the community... I'm looking forward to seeing you grow in your journey.
+ 4
This is an odd question. It's like finding part of a page from a book on the road and asking people to explain the meaning.
My recommendation is to include as much context as needed for people willing to and able to help.
+ 4
This is an example of a Python closure.
You can learn more about this in the following link:
https://www.programiz.com/python-programming/closure
+ 4
What makes it so difficult to understand is the poorly named functions and parameters.
I can't figure out what cons(a, b) is supposed to mean.
The nested function of pair(f) isn't much help either.
It does appear that the f argument is another function that will need to be passed in from the code that receives a reference to the inner pair(f) function.
So the client code would look something like:
def add(a, b):
return a+b;
def main():
some_pair = cons(1,2)
print(some_pair(add)) // outputs: 3
+ 4
HonFu Since the f variable is declared as a parameter, I'm guessing it's designed to support any number of functions.
Examples could be:
diff = some_pair(subtract)
product = some_pair(multiply)
ratio = some_pair(divide)
some_pair(log)
some_pair(display)
Each of the methods passed in as callback functions would have the same function signature as add(a, b).
Again, I'm completely speculating here.
+ 3
Abhinav Verma Hmmm... Was this some sort of challenge where you already know the answer, which you reveal after everyone has been stumped for a while? Is that what this was?
+ 3
David Carroll Thanks for the kind words.
+ 2
This code snip have some errors so you can't use it as it. Question is there more code with it?
+ 2
David Carroll, is it possible that the function name f is supposed to just be read into the function from the global scope without actually being passed as an argument?
+ 2
David Carroll I accept my mistake. Apology. I thank you and all other contributors to this thread. Next, I'll be clearer to what I wanted to ask.
There was no "confidence" as such. I am still learning and would take care next time for all the confusion I created with this thread.
Cheers!!!
0
There is nothing more to it. The given function/algo is to be implement just to contruct a pair
0
David Carroll this was no challenge but I don't know what f is doing when cons(a,b) returns a pair