unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: ludo@gnu.org (Ludovic Courtès)
To: guile-user@gnu.org
Subject: Re: Parameters
Date: Mon, 04 Feb 2008 15:28:43 +0100	[thread overview]
Message-ID: <87myqgx0bo.fsf@dellish.bordeaux.inria.fr> (raw)
In-Reply-To: 87lk603k3u.fsf@moley.moleskin.org

Hi,

Sebastian Tennant <sebyte@smolny.plus.com> 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)'.

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)))))))

Or you can just use `+'.  :-)

Hope this helps,
Ludovic.





  reply	other threads:[~2008-02-04 14:28 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-04 13:51 Parameters Sebastian Tennant
2008-02-04 14:28 ` Ludovic Courtès [this message]
2008-02-04 15:05   ` Parameters Sebastian Tennant
2008-02-04 15:29     ` Parameters Chusslove Illich
2008-02-04 15:45       ` Parameters Sebastian Tennant
2008-02-04 15:47     ` Parameters Ludovic Courtès
2008-02-04 17:22       ` Parameters Sebastian Tennant

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87myqgx0bo.fsf@dellish.bordeaux.inria.fr \
    --to=ludo@gnu.org \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).