From: SerGioGio
Subject: function definitions
Date: 
Message-ID: <9f84ph$8qi$1@arcturus.ciril.fr>
Hello,

This is a question about how function definition is done in Lisp.

in Lisp we can define a function muly like that :
"( defun muly (x) (* x y) )"
even if y was not "bound" before the statement.
then,
"(muly 1)" raises an error of course, y is still undefined.
If we try a "(setq y 2)", now "(muly 1)" works, replying 2 (!!).
If I change y, say "(setq y 9)", "(muly 1)" still works, but replies
something different : 9...
so in Lisp identifiers from the "body" of a function are not bound at
function declaration.

Now, in CAML:
"let muly = function x -> x * y;;" won't work at all : y is undefined.
I have to say : "let y = 2;;" and then "let muly = function x -> x * y;;"
works.
"muly(1)" replies 2 of course.
if I change the definition of y : "let y = 9;;", "muly(1);;" will still
reply 2.
so, in CAML, identifiers from the "body" of a function ARE bound at function
declaration.
(in Smalltalk too, even if there is no functions but "BlockClosures")

I haven't used much these two languages, so I may miss many things.
I guess you are all familiar with that, and I would like to know what in
your opinion is best.
So far, I found that :
* in the CAML method, you have to declare the local variables you will use.
* in the CAML method, you cannot access changing variables in a function :
you won't see the changes
* in the LISP method, your function definition can change any time...
In fact this raises the problem of what a function is exactly.
* in CAML, the result of a function depends only on the parameters.
* in LISP, you are sure of nothing whan your function has been defined : a
function is JUST an expression evaluated at a certain time.

Thanks for any remark on the problems caused by the two methods, and which
is "best" practically and theoritically !

SerGioGio
From: Marco Antoniotti
Subject: Re: function definitions
Date: 
Message-ID: <y6c66eg8ee8.fsf@octagon.mrl.nyu.edu>
"SerGioGio" <·············@wanadoo.fr> writes:

> Hello,
> 
> This is a question about how function definition is done in Lisp.
> 
> in Lisp we can define a function muly like that :
> "( defun muly (x) (* x y) )"

Too many spaces:

	(defun muly (x) (* x y))

> even if y was not "bound" before the statement.
> then,
> "(muly 1)" raises an error of course, y is still undefined.
> If we try a "(setq y 2)", now "(muly 1)" works, replying 2 (!!).
> If I change y, say "(setq y 9)", "(muly 1)" still works, but replies
> something different : 9...
> so in Lisp identifiers from the "body" of a function are not bound at
> function declaration.
> 
> Now, in CAML:
> "let muly = function x -> x * y;;" won't work at all : y is undefined.
> I have to say : "let y = 2;;"

Essentially you just defined a global variable.  Are you sure you need
one?  This question is not purely rethorical.

> and then "let muly = function x -> x * y;;"
> works.
> "muly(1)" replies 2 of course.
> if I change the definition of y : "let y = 9;;", "muly(1);;" will still
> reply 2.
> so, in CAML, identifiers from the "body" of a function ARE bound at function
> declaration.
> (in Smalltalk too, even if there is no functions but "BlockClosures")
> 
> I haven't used much these two languages, so I may miss many things.
> I guess you are all familiar with that, and I would like to know what in
> your opinion is best.
> So far, I found that :
> * in the CAML method, you have to declare the local variables you will use.
> * in the CAML method, you cannot access changing variables in a function :
> you won't see the changes
> * in the LISP method, your function definition can change any time...
> In fact this raises the problem of what a function is exactly.
> * in CAML, the result of a function depends only on the parameters.

Given your previous example this is an incorrect statement.  The
result of a function in (O)CAML depend on the parameters and the
"environment" it has been defined into.  Where you define "the
environment" as the set of "top level" bindings" you have at
definition time.  Therefore, (O)CAML lexically closes its functions in
the "defining environment".

> * in LISP, you are sure of nothing whan your function has been defined : a
> function is JUST an expression evaluated at a certain time.

In COMMON LISP (and this has been noted recently) the standard
essentially leaves open the interpretation that all "top level
environment bindings" are essentially "dynamic", hence, each top level
function definition can "dynamically" access bindings which were
"undefined" at function definition time.  Note that Common Lisp
compilers can catch this.

* (defun q (e) (+ e r))

Q
* (compile 'q)

In: LAMBDA (E)
  (+ E R)
Warning: Undefined variable: R


Warning: This variable is undefined:
  R


Compilation unit finished.
  2 warnings


Q
NIL
NIL
* 

Moreover, compare your CAML example with

	(let ((y 44))
           (defun m (x) (* x y)))

> 
> Thanks for any remark on the problems caused by the two methods, and which
> is "best" practically and theoritically !

I have noticed that many (O)CAML programs use an inordinate amount of
top level 'let' statements.  This is a consequence on part of the CAML
designers to do away with the exclusive use of 'fun/and' to define
functions.  Now, many such 'let' statements are legitimate, inasmuch
as they define functions.  However, this linguistic style may make it
too easy to abuse what, after all, are 'global' variables.

The bottom line is that you do not define many "global" variables in
Common Lisp programs, unless you know you want them dynamically bound
(at least this is my experience), hence the problem you pose is not
that big after all.  (And you still have S-exprs and you do not have
the type system tripping you every other step - this last remark to be
obviously intended as a provocation :) ).

Cheers

-- 
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group        tel. +1 - 212 - 998 3488
719 Broadway 12th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA                 http://bioinformatics.cat.nyu.edu
                    "Hello New York! We'll do what we can!"
                           Bill Murray in `Ghostbusters'.