[Python-ideas] return from -- breadth of usage

spir denis.spir at gmail.com
Mon Jan 20 16:29:39 CET 2014


I think tail call is very common. Consider following examples:

def perform (input):	# a "action"
     data = prepare(input)
     process(data)   # tail call

def result (input):	# a "function" properly speaking
     data = prepare(input)
     return process(data)   # tail call

def case1 (input):
     if cond(input):
         <deal with common case in place>
         return
     deal_with_special_case()    # tail call

def case2 (input):
     if cond(input):
         <deal with special case in place>
         return
     deal_with_common_case()     # tail call

def perform_cases (input):
     if cond1(input):
         case1(input)    # tail call
     elif cond2(input):
         case2(input)    # tail call
     elif cond3(input):
         case3(input)    # tail call

def result_cases (input):
     if cond1(input):
         return case1(input)    # tail call
     elif cond2(input):
         return case2(input)    # tail call
     elif cond3(input):
         return case3(input)    # tail call

There are probably many more typical *schemas* of common tail call use cases. It 
is in any case very frequent, of pretty various usage, and not specific to 
functional or functional-like programming. Instead, we all use tail calls 
constantly, without even thinking at it, just like we constantly make prose ;-). [1]

My point of view is not that tail call is a special (maybe very minoritary) kind 
of call, but that there are 2 kinds of calls maybe of equal importance:
* delegation: another proc is passed the responsability of performing a task, or 
achieving the rest of it (tail call)
* assistance: another proc is used to assist in a main task, still controlled 
and assumed by the main proc (sub call)

I guess there are 2 main situations of delegation: / tail calls
* the main proc sorts out cases and delegates in some or all cases
* the main proc prepares the task and a delegate achieves it
which may be mixed. (I may miss some, for sure.)

"return from" may well do the job, but entertains imo wrong views about tail 
calls. Maybe "pass" would do the job better. When a delegate f performs an 
action (action, examples 'perform' & 'perform_cases' & 'case*' above), it can be 
interpreted as "pass the responsability of the task to f", or just "pass by f". 
When a delegate f computes a result (function, examples 'result' & 
'result_cases' above) it can interpreted as "pass f's result back to the 
caller". (There is a similar ambiguity with "return", actually also matching 
semantic ambiguity.)

denis

[1] Allusion to https://en.wikipedia.org/wiki/Le_Bourgeois_gentilhomme


More information about the Python-ideas mailing list