home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / lisp / lispnews / text0241.txt < prev    next >
Encoding:
Text File  |  1985-11-10  |  1.7 KB  |  44 lines

  1. As McGeer@ucbkim points out, the code for assoc is (in common0.l):
  2.  
  3. (def assoc
  4.   (lambda (val alist)
  5.       (do ((al alist (cdr al)))
  6.           ((null al) nil)
  7.           (cond ((null (car al)))
  8.             ((not (dtpr (car al)))
  9.              (error "bad arg to assoc" al))
  10.             ((equal val (caar al)) (return (car al)))))))
  11.  
  12. The reason for the reported strange behavior of assoc is as simple as
  13. it is obscure!  First a couple hints:
  14.  - This code is inside the executable franz interpreter and therefore
  15.    runs compiled.
  16.  - The same strangeness is exhibited by assq, which is C-coded inside
  17.    the Franz kernel.
  18.  - Both the cdr of a dtpr and the value of an atom are stored at offset
  19.    zero within their respective structures.  The car and plist
  20.    similarly share the same offset within their structures.
  21.  - If you try to run the above assoc definition interpreted, it will
  22.    properly fail to work, or rather, it will complain about a bad arg
  23.    to car.
  24.  - Of course, c[ad]r in the interpreter carfully check their arguments;
  25.    In the compiler, they simple indirect through pointers for maximum
  26.    speed.
  27.  
  28. Finally, in case by now it isn't absolutely clear to everyone what is
  29. going on, assoc is (sort of) comparing val against the plist of bar and
  30. subsequently foo, and then each time automagically effectively doing a
  31. symeval on the atom to continue scanning down the "cdr" of the alist.
  32.  
  33. Moral:  Although the interpreter tries pretty hard to do the dynamic
  34. typing and implied typechecking for which lisp is famous, type checking
  35. in compiled code is often a myth.  Correct code only necessarily works
  36. correctly when given corrent arguments.
  37.  
  38. Steve Haflich
  39. MIT Experimental Music Studio
  40. smh%mit-ems@mit-mc
  41. {decvax!genrad, ihnp4!mit-eddie}!mit-ems!smh
  42.  
  43.  
  44.