unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Ricardo Wurmus <rekado@elephly.net>
To: Joshua Branson <jbranso@dismail.de>
Cc: 46014@debbugs.gnu.org
Subject: bug#46014: (define (thunk) (lambda (x) x)) should be a compile error?
Date: Thu, 21 Jan 2021 23:27:23 +0100	[thread overview]
Message-ID: <878s8mkjsk.fsf@elephly.net> (raw)
In-Reply-To: <87eeie2gs1.fsf@dismail.de>


Hi Joshua,

> When I look at
>
> #+BEGIN_SRC scheme
> (define (thunk)
>    (lambda (x)
>      x))
> #+END_SRC

[…]

> My thought is, this is clearly a mistake.  This person needs to change
> the above code.

How is this clearly a mistake?  The definition of “thunk” above is
perfectly fine and also common.

The above is equivalent to

   (define thunk
     (lambda ()
       (lambda (x) x)))

And that’s really okay and can be desired.   The problem is not with
this definition.  If someone calls this wrongly, well, that’s a problem
with the caller.  And Guile’s compiler does tell you that you are
probably wrong in calling “thunk” with an argument.

Do I understand you correctly that you would like this warning to be an
error instead?

> Gotcha.  Thanks for explaining!  I suppose what I meant to say is,
> should guile refuse to compile the above?  In other languages, like C I
> suppose, writing a function simultaneous with one and two arguments
> would refuse to compile.  The compiler would make you fix the code.

Let me address this separately.  In Scheme you *can* define a procedure
that takes a different number of arguments.  Here’s one example from the
manual:

     (define (make-accum n)
       (case-lambda
         (() n)
         ((m) (set! n (+ n m)) n)))

     (define a (make-accum 20))
     (a) ⇒ 20
     (a 10) ⇒ 30
     (a) ⇒ 30

“case-lambda” specifies a procedure that can take arguments in as many
different shapes as there are clauses.  Here there are two clauses: one
for the case where no arguments are provided and another where one
argument (bound to “m”) is provided.

Furthermore, you can see here that this is a higher order procedure, as
“make-accum” takes an argument and returns a procedure (the
case-lambda).

Another example, also from the manual, is this:

    (lambda* (start #:optional (end (+ 10 start)))
      (do ((i start (1+ i)))
          ((> i end))
        (display i)))

This procedure takes one or two arguments.

-- 
Ricardo





  reply	other threads:[~2021-01-21 22:27 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-21  2:04 bug#46014: (define (thunk) (lambda (x) x)) should be a compile error? jbranso--- via Bug reports for GUILE, GNU's Ubiquitous Extension Language
2021-01-21 13:56 ` Ricardo Wurmus
2021-01-21 18:16   ` Stefan Israelsson Tampe
2021-01-21 20:11     ` jbranso--- via Bug reports for GUILE, GNU's Ubiquitous Extension Language
2021-01-21 20:09   ` jbranso--- via Bug reports for GUILE, GNU's Ubiquitous Extension Language
2021-01-21 22:27     ` Ricardo Wurmus [this message]
2021-01-22 14:47       ` jbranso--- via Bug reports for GUILE, GNU's Ubiquitous Extension Language
2021-01-22 16:29         ` tomas
2021-01-23 15:02         ` Ricardo Wurmus
2021-01-23 15:17           ` jbranso--- via Bug reports for GUILE, GNU's Ubiquitous Extension Language
2021-01-23 17:23             ` Ricardo Wurmus
2021-01-23 15:07 ` bug#46014: closing the bug report...hopefully jbranso--- via Bug reports for GUILE, GNU's Ubiquitous Extension Language

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=878s8mkjsk.fsf@elephly.net \
    --to=rekado@elephly.net \
    --cc=46014@debbugs.gnu.org \
    --cc=jbranso@dismail.de \
    /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).