bug-guile
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#46014: (define (thunk) (lambda (x) x)) should be a compile error?


From: Ricardo Wurmus
Subject: bug#46014: (define (thunk) (lambda (x) x)) should be a compile error?
Date: Thu, 21 Jan 2021 23:27:23 +0100
User-agent: mu4e 1.4.13; emacs 27.1

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 via email to

[Prev in Thread] Current Thread [Next in Thread]