From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Mark H Weaver Newsgroups: gmane.lisp.guile.user Subject: Re: Multiple values passed as single argument to procedure Date: Sun, 11 Jun 2017 17:31:59 -0400 Message-ID: <87efuq2ots.fsf@netris.org> References: <87mv9fnejc.fsf@gmail.com> <87k24i2rev.fsf@netris.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1497216772 27047 195.159.176.226 (11 Jun 2017 21:32:52 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 11 Jun 2017 21:32:52 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux) Cc: guile-user@gnu.org To: Chris Marusich Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Sun Jun 11 23:32:47 2017 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dKATW-0006dx-4t for guile-user@m.gmane.org; Sun, 11 Jun 2017 23:32:46 +0200 Original-Received: from localhost ([::1]:35240 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dKATb-00004y-9e for guile-user@m.gmane.org; Sun, 11 Jun 2017 17:32:51 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:51554) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dKAT2-0008WN-Ji for guile-user@gnu.org; Sun, 11 Jun 2017 17:32:17 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dKASz-0002PF-Cb for guile-user@gnu.org; Sun, 11 Jun 2017 17:32:16 -0400 Original-Received: from world.peace.net ([50.252.239.5]:42514) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dKASz-0002Oo-7e for guile-user@gnu.org; Sun, 11 Jun 2017 17:32:13 -0400 Original-Received: from pool-72-93-31-241.bstnma.east.verizon.net ([72.93.31.241] helo=jojen) by world.peace.net with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dKASx-0001I0-Qe; Sun, 11 Jun 2017 17:32:12 -0400 In-Reply-To: <87k24i2rev.fsf@netris.org> (Mark H. Weaver's message of "Sun, 11 Jun 2017 16:36:08 -0400") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 50.252.239.5 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:13817 Archived-At: I wrote: > Use 'call-with-values', 'let-values', or 'receive' to call a procedure > that returns multiple values (or no values). > > If you do not use one of the above forms (or a macro that expands to one > of them) to call a procedure that returns multiple values, then Guile > will discard all but the first result. Note that this is a > Guile-specific extension. Other Scheme implementations may behave > differently (e.g. report an error) if multiple values (or no values) are > returned to a procedure call that was not done using one of the forms > listed above. I forgot to mention that procedure calls in "tail position" are a special case known as "tail calls". In Scheme such a call is semantically a "GOTO with arguments". In such cases, the caller procedure (let's call it FOO) has nothing left to do after its callee (let's call it BAR) returns, and can simply arrange for BAR to return its values directly to the caller of FOO. In such cases, BAR can return as many values as FOO's caller is prepared to accept. For example: (define (div-and-mod x y) (if (negative? y) (ceiling/ x y) (floor/ x y))) (define (div x y) (let-values (((q r) (div-and-mod x y))) q)) Here, both 'floor/' and 'ceiling/' return two values (see the Guile manual). Since both of those procedures are called from tail position in 'div-and-mod', they are tail calls (semantically GOTO with arguments), and they return their values directly to the caller of 'div-and-mod'. FYI, the 'div-and-mod' and 'div' defined above are equivalent to the standard procedures of the same name in R6RS scheme, and to 'euclidean/' and 'euclidean-quotient' included in core Guile. Incidentally, the fact that tail calls are semantically GOTOs with arguments, and that this behavior is guaranteed and not a mere "tail call optimization", is the reason that we are able to write loops in Scheme as recursive calls without using unbounded stack space. In fact, Scheme has no loop facility in the core language. Mark