home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!spool.mu.edu!think.com!barmar
- From: barmar@think.com (Barry Margolin)
- Newsgroups: comp.lang.lisp
- Subject: Re: Lucid Lisp complains about this code fragment
- Date: 5 Jan 1993 09:21:36 GMT
- Organization: Thinking Machines Corporation, Cambridge MA, USA
- Lines: 47
- Message-ID: <1ibjv0INN2pk@early-bird.think.com>
- References: <1iat18INNii@MINERVA.CIS.YALE.EDU>
- NNTP-Posting-Host: gandalf.think.com
-
- In article <1iat18INNii@MINERVA.CIS.YALE.EDU> theodore-michael@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.
-
- barmar@think.com {uunet,harvard}!think!barmar
-