From: Michael Theodore
Subject: Lucid Lisp complains about this code fragment
Date: 
Message-ID: <1iat18INNii@MINERVA.CIS.YALE.EDU>
Any thoughts on how to correct this?
For some reason, what follows is no problem on Vax Lisp, nor on Macintosh
Common Lisp, but when run on Lucid Lisp gets a warning :
;;Warning function tweak defined more than once 

I wouldn't care if it weren't for the fact that warning is printed to the 
screen repeatedly (about 50 times).

(defun addNoiseToListOfNumbers (l)
  (defun tweak (x)
    (+
     x
     (- (* (random 1000) 0.0001) 0.05)))
  (mapcar #'tweak l))

Please excuse my ignorance if this is a very obvious problem. 

	Thanks,
	Michael


(BTW, this function is from some code in a book by Mark Watson entitled
"Common Lisp Modules - Artificial Intelligence in the Era of Neural Networks
and Chaos Theory").

-- 
Practice random acts of randomness.........
		················@cs.yale.edu

From: Barry Margolin
Subject: Re: Lucid Lisp complains about this code fragment
Date: 
Message-ID: <1ibjv0INN2pk@early-bird.think.com>
In article <···········@MINERVA.CIS.YALE.EDU> ················@yale.edu (Michael Theodore) writes:
>Any thoughts on how to correct this?
>For some reason, what follows is no problem on Vax Lisp, nor on Macintosh
>Common Lisp, but when run on Lucid Lisp gets a warning :
>;;Warning function tweak defined more than once 
>
>I wouldn't care if it weren't for the fact that warning is printed to the 
>screen repeatedly (about 50 times).
>
>(defun addNoiseToListOfNumbers (l)
>  (defun tweak (x)
>    (+
>     x
>     (- (* (random 1000) 0.0001) 0.05)))
>  (mapcar #'tweak l))

Some Lisp implementations don't deal very well with DEFUNs other than at
top level.

However, this code is pretty silly.  Every time you run it, TWEAK is
potentially redefined.  And since TWEAK contains no references to any
variables local to addNoseToListOfNumbers, there's no reason for it to be
defined inside that function.  Unless you redefine TWEAK in between calls
to addNoiseToListOfNumbers, it's exactly equivalent to:

(defun tweak (x)
  (+ x (- (* (random 1000) .0001) .05)))
(defun addNoiseToListOfNumbers (l)
  (mapcar #'tweak l))

It looks to me like someone naively translated that from Scheme.  In Common
Lisp, DEFUN always assigns the global function binding, while in Scheme a
DEFINE inside another DEFINE creates a local binding (in both languages,
the body of the function is executed in the local environment).  In Common
Lisp, if you don't want TWEAK to be in the global namespace, you should
write:

(defun add-noise-to-list-of-numbers (l)
  (flet ((tweak (x)
           (+ x
	      (- (* (random 1000) .0001) .05))))
    (mapcar #'tweak l)))
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar
From: hume smith
Subject: Re: Re: Lucid Lisp complains about this code fragment
Date: 
Message-ID: <21495.1993Jan5.094839@bcars148>
In <············@early-bird.think.com>, ······@think.com (Barry Margolin) said:
> In Common
> Lisp, if you don't want TWEAK to be in the global namespace, you should
> write:
> 
> (defun add-noise-to-list-of-numbers (l)
>   (flet ((tweak (x)
>            (+ x
> 	      (- (* (random 1000) .0001) .05))))
>     (mapcar #'tweak l)))

you could also say

(defun add-noise-to-list-of-numbers (l)
  (mapcar #'(lambda (x) (+ x (* (random 1000) .0001) -.05)) l))

but that may be less efficient - for instance, in XLisp, each time
add-noise-to-list-of-numbers is called a new closure is made.  i think.
--
Hume Smith                      Wenn ich des Tages nicht dreimal
··········@acadiau.ca           mein Schaelchen Coffee trinken darf,
······@bnr.ca                   so werd' ich ju zu meiner Qual
                                wie ein verdorrtes Ziegenbraetchen.