home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / lisp / 3168 < prev    next >
Encoding:
Text File  |  1993-01-05  |  2.1 KB  |  59 lines

  1. Path: sparky!uunet!spool.mu.edu!think.com!barmar
  2. From: barmar@think.com (Barry Margolin)
  3. Newsgroups: comp.lang.lisp
  4. Subject: Re: Lucid Lisp complains about this code fragment
  5. Date: 5 Jan 1993 09:21:36 GMT
  6. Organization: Thinking Machines Corporation, Cambridge MA, USA
  7. Lines: 47
  8. Message-ID: <1ibjv0INN2pk@early-bird.think.com>
  9. References: <1iat18INNii@MINERVA.CIS.YALE.EDU>
  10. NNTP-Posting-Host: gandalf.think.com
  11.  
  12. In article <1iat18INNii@MINERVA.CIS.YALE.EDU> theodore-michael@yale.edu (Michael Theodore) writes:
  13. >Any thoughts on how to correct this?
  14. >For some reason, what follows is no problem on Vax Lisp, nor on Macintosh
  15. >Common Lisp, but when run on Lucid Lisp gets a warning :
  16. >;;Warning function tweak defined more than once 
  17. >
  18. >I wouldn't care if it weren't for the fact that warning is printed to the 
  19. >screen repeatedly (about 50 times).
  20. >
  21. >(defun addNoiseToListOfNumbers (l)
  22. >  (defun tweak (x)
  23. >    (+
  24. >     x
  25. >     (- (* (random 1000) 0.0001) 0.05)))
  26. >  (mapcar #'tweak l))
  27.  
  28. Some Lisp implementations don't deal very well with DEFUNs other than at
  29. top level.
  30.  
  31. However, this code is pretty silly.  Every time you run it, TWEAK is
  32. potentially redefined.  And since TWEAK contains no references to any
  33. variables local to addNoseToListOfNumbers, there's no reason for it to be
  34. defined inside that function.  Unless you redefine TWEAK in between calls
  35. to addNoiseToListOfNumbers, it's exactly equivalent to:
  36.  
  37. (defun tweak (x)
  38.   (+ x (- (* (random 1000) .0001) .05)))
  39. (defun addNoiseToListOfNumbers (l)
  40.   (mapcar #'tweak l))
  41.  
  42. It looks to me like someone naively translated that from Scheme.  In Common
  43. Lisp, DEFUN always assigns the global function binding, while in Scheme a
  44. DEFINE inside another DEFINE creates a local binding (in both languages,
  45. the body of the function is executed in the local environment).  In Common
  46. Lisp, if you don't want TWEAK to be in the global namespace, you should
  47. write:
  48.  
  49. (defun add-noise-to-list-of-numbers (l)
  50.   (flet ((tweak (x)
  51.            (+ x
  52.           (- (* (random 1000) .0001) .05))))
  53.     (mapcar #'tweak l)))
  54. -- 
  55. Barry Margolin
  56. System Manager, Thinking Machines Corp.
  57.  
  58. barmar@think.com          {uunet,harvard}!think!barmar
  59.