[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

what is case-lambda

Jim Newton

4/5/2016 9:49:00 AM

Does anyone here have expertise with R7RS scheme?
I'm trying to read something from the spec, and I don't 100% understand.

There is a spec for case-lambda in section 4.2.9 of
http://trac.sacrideo.us/wg/raw-attachment/wiki/WikiStar...

It is unclear to me whether case-lambda only dispatches based on the number
of arguments (number of elements of the argument list), or whether
it also does some destructuring-bind type analysis as well.

I.e., can I have a clause of case-lambda like...

(case-lambda
((a b (c d (e f))) ...))

If no one knows on comp.lang.lisp, I can try on some scheme group...


4.2.9. Case-lambda
(case-lambda ?clause? . . . ) case-lambda library syntax
Syntax: Each ?clause? is of the form (?formals? ?body?), where ?formals? and ?body? have the same syntax as in a lambda expression.
Semantics: A case-lambda expression evaluates to a pro- cedure that accepts a variable number of arguments and is lexically scoped in the same manner as a procedure re- sulting from a lambda expression. When the procedure is called, the first ?clause? for which the arguments agree with ?formals? is selected, where agreement is specified as for the ?formals? of a lambda expression. The variables of ?formals? are bound to fresh locations, the values of the arguments are stored in those locations, the ?body? is evaluated in the extended environment, and the results of ?body? are returned as the results of the procedure call.
It is an error for the arguments not to agree with the ?formals? of any ?clause?.
(define range
(case-lambda
((e) (range 0 e))
((b e) (do ((r ’() (cons e r))
(let ((a 3)) (list (list 1 2) a 4 ’five 6))
(?keyword? ?datum? ...)
(range 3)
(range 3 5)
4.3. Macros
(e (- e 1) (- e 1)))
((< e b) r)))))
=? (0 1 2) =? (3 4)
10 Answers

Jussi Piitulainen

4/5/2016 10:26:00 AM

0

Jim Newton writes:

> Does anyone here have expertise with R7RS scheme?
> I'm trying to read something from the spec, and I don't 100% understand.
>
> There is a spec for case-lambda in section 4.2.9 of
> http://trac.sacrideo.us/wg/raw-attachment/wiki/WikiStar...
>
> It is unclear to me whether case-lambda only dispatches based on the
> number of arguments (number of elements of the argument list), or
> whether it also does some destructuring-bind type analysis as well.
>
> I.e., can I have a clause of case-lambda like...
>
> (case-lambda
> ((a b (c d (e f))) ...))
>
> If no one knows on comp.lang.lisp, I can try on some scheme group...

The formals have to be one of the three forms allowed in a lambda
expression: (variable0 ...), (variable0 variable1 ... . variable),
variable, where ellipsis indicates zero or more of the previous item. No
destructuring.

> 4.2.9. Case-lambda
> (case-lambda â?¨clauseâ?© . . . ) case-lambda library syntax
> Syntax: Each â?¨clauseâ?© is of the form (â?¨formalsâ?© â?¨bodyâ?©), where
> â?¨formalsâ?© and â?¨bodyâ?© have the same syntax as in a lambda expression.

I'm not sure how that is not clear.

[- -]

Jim Newton

4/5/2016 12:05:00 PM

0



>
> > 4.2.9. Case-lambda
> > (case-lambda ?clause? . . . ) case-lambda library syntax
> > Syntax: Each ?clause? is of the form (?formals? ?body?), where
> > ?formals? and ?body? have the same syntax as in a lambda expression.
>
> I'm not sure how that is not clear.
>

The reason it was not clear to me is that I didn't know what <formals> was. It is clearly
at least a list of symbols indicating variable names, but whether <formals> allows other
syntax such as optional arguments, or keywords, or destructuring is something a schemer
would readily know, and is likely described elsewhere in the language specification.

Barry Margolin

4/5/2016 3:21:00 PM

0

In article <dd828fe7-bb6f-44c2-bbea-88e4d58def68@googlegroups.com>,
Jim Newton <jimka.issy@gmail.com> wrote:

> Does anyone here have expertise with R7RS scheme?

comp.lang.scheme would be a better place to ask.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Chris Vine

4/5/2016 9:43:00 PM

0

On Tue, 5 Apr 2016 05:04:36 -0700 (PDT)
Jim Newton <jimka.issy@gmail.com> wrote:
> >
> > > 4.2.9. Case-lambda
> > > (case-lambda ?clause? . . . ) case-lambda library syntax
> > > Syntax: Each ?clause? is of the form (?formals? ?body?), where
> > > ?formals? and ?body? have the same syntax as in a lambda
> > > expression.
> >
> > I'm not sure how that is not clear.
> >
>
> The reason it was not clear to me is that I didn't know what
> <formals> was. It is clearly at least a list of symbols indicating
> variable names, but whether <formals> allows other syntax such as
> optional arguments, or keywords, or destructuring is something a
> schemer would readily know, and is likely described elsewhere in the
> language specification.

There is no destructuring. There is just matching by argument numbers.

Incidentally in scheme it is a list of identifiers rather than a list
of symbols: see the first paragraph of section 3.1 of R7RS. Identifiers
denote symbols when they appear as or within a literal (section 2.1).
Not that it makes any practical difference - the effect is the same and
it is just semantics. Scheme avoids mixing the implementation with the
specification.

Chris

Kaz Kylheku

4/6/2016 5:14:00 AM

0

On 2016-04-05, Chris Vine <chris@cvine--nospam--.freeserve.co.uk> wrote:
> On Tue, 5 Apr 2016 05:04:36 -0700 (PDT)
> Jim Newton <jimka.issy@gmail.com> wrote:
>> >
>> > > 4.2.9. Case-lambda
>> > > (case-lambda â?¨clauseâ?© . . . ) case-lambda library syntax
>> > > Syntax: Each â?¨clauseâ?© is of the form (â?¨formalsâ?© â?¨bodyâ?©), where
>> > > â?¨formalsâ?© and â?¨bodyâ?© have the same syntax as in a lambda
>> > > expression.
>> >
>> > I'm not sure how that is not clear.
>> >
>>
>> The reason it was not clear to me is that I didn't know what
>> <formals> was. It is clearly at least a list of symbols indicating
>> variable names, but whether <formals> allows other syntax such as
>> optional arguments, or keywords, or destructuring is something a
>> schemer would readily know, and is likely described elsewhere in the
>> language specification.
>
> There is no destructuring. There is just matching by argument numbers.
>
> Incidentally in scheme it is a list of identifiers rather than a list
> of symbols: see the first paragraph of section 3.1 of R7RS. Identifiers
> denote symbols when they appear as or within a literal (section 2.1).

We could call this a list of "idiotifiers".

> Not that it makes any practical difference - the effect is the same and
> it is just semantics. Scheme avoids mixing the implementation with the
> specification.

Is there no practical difference?

Look, either there is a purpose behind it or there isn't.

If there isn't, it is purposeless twaddle that should be moved
to an appendix---which could then be subject to a swift, benign
appendectomy.

If there is a purpose to the separation, it could only be so that so
syntax and its identifiers could be implemented *other* than with
symbols. For example, some goddamned YYLVAL nodes in a Yacc parser where
identifiers are "char *" strings coming out of strdup, or whatever.
(Haskell-envying garbage, basically). In any case such a thing is done,
it is undeniably a practical difference. It's a difference, and it's put
into practice.

A Lisp dialect should be specified so that such is explicitly ruled out.
If there is any threat of such a thing creeping in, a defensive move
must be made. For instance, a *parser-hook* mechanism can be defined
such that every piece of syntax can pass through a user-defined
function, which can validate that every identifier in the structure is
in fact a symbol object.

Kalle Olavi Niemitalo

4/6/2016 9:39:00 PM

0

Chris Vine <chris@cvine--nospam--.freeserve.co.uk> writes:

> Incidentally in scheme it is a list of identifiers rather than a list
> of symbols: see the first paragraph of section 3.1 of R7RS. Identifiers
> denote symbols when they appear as or within a literal (section 2.1).

Oh, that's too bad. From "The rules for writing a symbol" in
section 6.5 of R7RS, I got the impression that a sequence of
characters could be parsed as an identifier or as a symbol
depending on the context, but not as both. That would then allow
a perverse implementation to make #!fold-case affect identifiers
but not symbols. Alas, that idea did not survive sections 2.1
and 7.1.1.

(Is â?¹intertoken spaceâ?º defined somewhere?)

Jim Newton

4/7/2016 2:44:00 PM

0

On Wednesday, April 6, 2016 at 7:14:31 AM UTC+2, Kaz Kylheku wrote:

> >> > > 4.2.9. Case-lambda
> >> > > (case-lambda ?clause? . . . ) case-lambda library syntax
> >> >
> >> > I'm not sure how that is not clear.
> >> >
> >>
> >> The reason it was not clear to me is that I didn't know what
> >> <formals> was. It is clearly at least a list of symbols indicating
> >> variable names, but whether <formals> allows other syntax such as

Can't the <formals> of a lambda expression end with . var to sort of mean &rest var ?
Or is that only in my grandfather's scheme?

In this case case-lambda would support a small bit of destructuring? right?

Chris Vine

4/7/2016 4:18:00 PM

0

On Thu, 7 Apr 2016 07:43:47 -0700 (PDT)
Jim Newton <jimka.issy@gmail.com> wrote:
> On Wednesday, April 6, 2016 at 7:14:31 AM UTC+2, Kaz Kylheku wrote:
>
> > >> > > 4.2.9. Case-lambda
> > >> > > (case-lambda ?clause? . . . ) case-lambda library syntax
> > >> >
> > >> > I'm not sure how that is not clear.
> > >> >
> > >>
> > >> The reason it was not clear to me is that I didn't know what
> > >> <formals> was. It is clearly at least a list of symbols
> > >> indicating variable names, but whether <formals> allows other
> > >> syntax such as
>
> Can't the <formals> of a lambda expression end with . var to sort of
> mean &rest var ? Or is that only in my grandfather's scheme?
>
> In this case case-lambda would support a small bit of destructuring?
> right?

The . operator in the formals is for this purpose the same as &rest.
Matching of the number of arguments given takes place against the
formals clauses, in order, and the first matching clause is selected.
So a formals clause with a rest parameter will match any number of
arguments equal to or greater than the number of non-rest parameters.
Since you have R7RS, you can read all about it yourself.

There is a sense in which you could say that non-rest parameters
destructure, but that is equally true of any function call.

There is no big deal about case-lambda. Its main purpose is to save
you having a rest parameter in simple cases and then having to
manipulate the rest list by hand.

Kaz Kylheku

4/7/2016 7:27:00 PM

0

On 2016-04-07, Jim Newton <jimka.issy@gmail.com> wrote:
> On Wednesday, April 6, 2016 at 7:14:31 AM UTC+2, Kaz Kylheku wrote:
>
>> >> > > 4.2.9. Case-lambda
>> >> > > (case-lambda â?¨clauseâ?© . . . ) case-lambda library syntax
>> >> >
>> >> > I'm not sure how that is not clear.
>> >> >
>> >>
>> >> The reason it was not clear to me is that I didn't know what
>> >> <formals> was. It is clearly at least a list of symbols indicating
>> >> variable names, but whether <formals> allows other syntax such as
>
> Can't the <formals> of a lambda expression end with . var to sort of mean &rest var ?
> Or is that only in my grandfather's scheme?
>
> In this case case-lambda would support a small bit of destructuring? right?

(lambda (a b . c) ...) Means that the argument list (1 2 3 4 5) is
"destructured" such that (a b) takes (1 2), and c takes the rest:
(3 4 5).

This is not really destructuring because c cannot match an atom
in Scheme (I think).

In TXR Lisp, I made it a rule that compound forms *can* in fact be
improper lists, even function calls.

In a nutshell, what this means in the case of a function call is
that the form

(foo a b c . d)

basically gives you a shorthand for this:

(apply (fun foo) a b c d)

If foo takes exactly three required arguments and one rest,
then d can evaluate to an atom. The rest will get that atom.

It will also create an improper list which can bind to the
rest parameter. This won't work in CL:

1> (list 1 2 . 3)
(1 2 . 3)
2> (apply (fun list) 1 2 3)
(1 2 . 3)

TXR Lisp has list*, but you don't need it:

(list* 1 2 3) -> (1 2 . 3)

(cons 1 2) -> (1 . 2)

(list 1 2 . 3) -> (1 2 . 3)

(list 1 . 2) -> (1 . 2)

Now what happens if you dot the list*? Finally, we arrive
at a situation in which a complaint is entirely justified:

1> (list* 1 2 . 3)
** 3 is not a cons
** during evaluation at expr-1:1 of form (list* 1 2 . 3)

Not the best imaginable error message; ah well.

Jim Newton

4/8/2016 7:27:00 AM

0

So as I originally suspected, <formals> is not JUST a list of variable names,
rather it is a list of variable names with some additional syntax.