Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'prefix': 0.07; 'finite': 0.09; 'loop.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:= 70': 0.10; 'python': 0.10; 'jan': 0.11; 'def': 0.13; 'auxiliary': 0.16; 'bytes),': 0.16; 'iterables': 0.16; 'iteration': 0.16; 'lisp': 0.16; 'obviously,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'slicing,': 0.16; 'wrote:': 0.16; 'bytes': 0.18; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'translated': 0.27; 'equivalent.': 0.29; 'code:': 0.29; 'subject:/': 0.30; 'list': 0.34; 'skip:i 20': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'requirement': 0.37; 'or,': 0.38; 'to:addr:python.org': 0.40; 'challenge': 0.61; 'avoid': 0.61; 'more': 0.63; 'necessarily': 0.63; 'believe': 0.66; 'clearer': 0.84; 'received:fios.verizon.net': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Possibly Pythonic Tail Call Optimization (TCO/TRE) Date: Mon, 13 Jul 2015 23:36:44 -0400 References: <55A3A853.4040006@rece.vub.ac.be> <55A3C366.6060602@rece.vub.ac.be> <55A3CE62.6060304@rece.vub.ac.be> <87r3obev0s.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-98-114-97-173.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 In-Reply-To: <87r3obev0s.fsf@elektro.pacujo.net> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1436845030 news.xs4all.nl 2853 [2001:888:2000:d::a6]:48209 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93774 On 7/13/2015 3:07 PM, Marko Rauhamaa wrote: > Or, translated into (non-idiomatic) Python code: > > ======================================================================== > def common_prefix_length(bytes_a, bytes_b): > def loop(list_a, list_b, common_length): > if not list_a: > return common_length > if not list_b: > return common_length > if list_a[0] == list_b[0]: > return loop(list_a[1:], list_b[1:], common_length + 8) > return common_length + \ > bit_match[8 - integer_length(list_a[0] ^ list_b[0])] > return loop(bytes_a, bytes_b, 0) > ======================================================================== This is an interesting challenge for conversion. The straightforward while loop conversion is (untested, obviously, without the auxiliary functions): def common_prefix_length(bytes_a, bytes_b): length = 0 while bytes_a and bytes_b and bytes_a[0] == bytes_b[0]: length += 8 bytes_a, bytes_b = bytes_a[1:], bytes_b[1:] if not bytes_a or not bytes_b: return length else: return common_length + bit_match[ 8 - integer_length(bytes_a[0] ^ bytes_b[0])] Using a for loop and zip to do the parallel iteration for us and avoid the list slicing, which is O(n) versus the O(1) lisp (cdr bytes), I believe the following is equivalent. def common_prefix_length(bytes_a, bytes_b): length = 0 for a, b in zip(bytes_a, bytes_b): if a == b: length += 8 else: return length + bit_match[8 - integer_length(a ^ b)] else: # short bytes is prefix of longer bytes return length I think this is much clearer than either recursion or while loop. It is also more general, as the only requirement on bytes_a and bytes_b is that they be iterables of bytes with a finite common_prefix, and not necessarily sequences with slicing or even indexing. -- Terry Jan Reedy