From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Sebastian Tennant Newsgroups: gmane.lisp.guile.user Subject: Re: Parameters Date: Mon, 04 Feb 2008 17:05:35 +0200 Message-ID: <8763x43gow.fsf@moley.moleskin.org> References: <87lk603k3u.fsf@moley.moleskin.org> <87myqgx0bo.fsf@dellish.bordeaux.inria.fr> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1202137615 25391 80.91.229.12 (4 Feb 2008 15:06:55 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 4 Feb 2008 15:06:55 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Mon Feb 04 16:07:17 2008 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1JM2uZ-0001sx-3N for guile-user@m.gmane.org; Mon, 04 Feb 2008 16:07:07 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JM2u7-0005T5-3x for guile-user@m.gmane.org; Mon, 04 Feb 2008 10:06:39 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1JM2tP-0004fD-Cc for guile-user@gnu.org; Mon, 04 Feb 2008 10:05:55 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1JM2tN-0004dO-8u for guile-user@gnu.org; Mon, 04 Feb 2008 10:05:54 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JM2tM-0004dC-WF for guile-user@gnu.org; Mon, 04 Feb 2008 10:05:53 -0500 Original-Received: from main.gmane.org ([80.91.229.2] helo=ciao.gmane.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1JM2tM-00054l-Mi for guile-user@gnu.org; Mon, 04 Feb 2008 10:05:53 -0500 Original-Received: from list by ciao.gmane.org with local (Exim 4.43) id 1JM2tI-0005wP-PC for guile-user@gnu.org; Mon, 04 Feb 2008 15:05:50 +0000 Original-Received: from 85.105.17.65 ([85.105.17.65]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 04 Feb 2008 15:05:48 +0000 Original-Received: from sebyte by 85.105.17.65 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 04 Feb 2008 15:05:48 +0000 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 65 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 85.105.17.65 User-Agent: Gnus/5.110007 (No Gnus v0.7) Emacs/22.1 (gnu/linux) Cancel-Lock: sha1:QNet87L3rokp+DwsRQyHgb2RhSI= X-detected-kernel: by monty-python.gnu.org: Linux 2.6, seldom 2.4 (older, 4) X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:6402 Archived-At: Quoth ludo@gnu.org (Ludovic Courtès): > Sebastian Tennant writes: >> This simple procedure behaves as expected, i.e., it provides the sum of >> a list of numbers: >> >> (define add >> (lambda (l) >> (if (null? l) >> 0 >> (+ (add (cdr l)) (car l))))) >> >> whereas this procedure results in a stack overflow: >> >> (define add >> (lambda l >> (if (null? l) >> 0 >> (+ (add (cdr l)) (car l))))) >> >> the only difference being the designation of the formal parameter of the >> anonymous procedure; l or (l). > > When creating a procedure with `(lambda args body...)', then, no matter > how it is invoked, ARGS will always be bound to the *list* of arguments > that were passed to the procedure. Consequently, such procedures can be > passed any numbers of arguments. > > Conversely, `(lambda (x) body...)' is a one-argument procedure. When it > is invoked, X is bound to its argument. > > Thus, in your case, the first `add' would be used with a single > argument, e.g., `(add '(1 2 3 4))', while the second would be used with > an indefinite number of arguments, e.g., `(add 1 2 3 4)'. Understood... up to a point. However, I don't understand why calling the first procedure like this: (add '(1 2 3 4)) and calling the second procedure like this: (add 1 2 3 4) doesn't result in 'l' having the same value; '(1 2 3 4), in each case? To put it another way, I would expect both procedures to work provided they are called with (a) suitable argument(s). Why does the second procedure fail regardless of how it is called? > Note that none of your procedures is tail-recursive, so they consume > stack space proportional to the length of L, which can lead to a stack > overflow for large lists. A tail-recursive definition would be: > > (define add > (lambda (l) > (let loop ((l l) > (result 0)) > (if (null? l) > result > (loop (cdr l) (+ result (car l))))))) Noted. Sebastian