ENOSIG Discussie (threads)


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

Re: scheme vraagje: is (accumulate cons nil sequence) zinvol?


On Wed, Sep 15, 2004 at 12:35:03AM +0200, Jan Nieuwenhuizen wrote:
> Lionel Elie Mamane writes:

>> Let's look at an example: let's suppose I want to increment every
>> element of the list l. Without curryfication, I have to write
>> something like
>>
>>   (map (lambda (x) (+ 1 x)) l)
>>
>> with curryfication, I would write:
>>
>>   (map (+ 1) l)

> Yes, I see; that would be nice.

>> which is much shorter and much more readable. Here you see that
>> actually, curryfication conflicts with a feature of Lispy languages:
>> variable number of arguments.

> Aha, indeed.  That's unfortunate, because variable number of
> arguments is quite a feature too?

Yes and no. There are in effect two types of "functions with variable
numbers of arguments":

 - Functions of a given number of arguments where some arguments are
   optional and take a default value if not given. Example: display,
   that takes something to display and an optional port to print to;
   defaults to stdout.

 - Functions, like +, that do something sensible out of an arbitrary
   number of arguments.

In Lisp specifically, the second type improves readability a lot:
saves parentheses! Compare (+ (+ (+ (+ (+ 1 2) 3) 4) 5) 6) and
(+ 1 2 3 4 5 6). But in a language with infix operators, the point is
more moot: 1+2+3+4+5+6 is quite OK, too. Besides that kind of
"multiple arguments" is hard (impossible?) to fit in a strong static
type system. One some level, the + of Scheme is actually the
(List.left_fold (+) 0) of OCaml ("(+)" here is the functions of two
arguments). 

As for the first type, one can get away with defining both a "display
to stdin" and a "display to a port" function, just like C has printf
and fprintf. <tiny>And actually, OCaml has support for optional
arguments. See "Function application" in
http://caml.inria.fr/ocaml/htmlman/manual015.html and "Optional
arguments" in http://caml.inria.fr/ocaml/htmlman/manual006.html. I
tend not to use it much, precisely because of the bad interaction with
partial application. And I find this "new" labels stuff a bit
unwieldy. Maybe I'll grow used to it someday and like it.</tiny>

<<inline: signature.asc>>


Gerelateerd:


[ Date Index] [ Thread Index]