home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / lisp / 3126 < prev    next >
Encoding:
Text File  |  1992-12-22  |  3.1 KB  |  86 lines

  1. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!jvnc.net!rutgers!psuvax1!news.ecn.bgu.edu!uxa.ecn.bgu.edu!news.ils.nwu.edu!aristotle.ils.nwu.edu!will
  2. From: will@aristotle.ils.nwu.edu (William Fitzgerald)
  3. Newsgroups: comp.lang.lisp
  4. Subject: Re: Use of format or pprint
  5. Message-ID: <1992Dec21.224457.23526@ils.nwu.edu>
  6. Date: 21 Dec 92 22:44:57 GMT
  7. References: <1gtulrINN3gr@early-bird.think.com>
  8. Sender: usenet@ils.nwu.edu (Mr. usenet)
  9. Distribution: usa
  10. Organization: The Institute for the Learning Sciences
  11. Lines: 71
  12. Nntp-Posting-Host: aristotle.ils.nwu.edu
  13. X-Newsreader: Tin 1.1 PL5
  14.  
  15. Barry Margolin (barmar@think.com) wrote:
  16. : You'll have to write your own routine to convert the single-string form
  17. : into a list of strings.  Common Lisp has nothing that will parse the string
  18. : into words and treat them separately for this purpose (the only thing in CL
  19. : that deals with words is STRING-CAPITALIZE).
  20.  
  21. Here are some functions I've written to convert strings into lists of
  22. various sorts. Examples follow at the end.
  23.  
  24. (defun list-from-string (string
  25.                          &key 
  26.                          (start 0) 
  27.                          (char-bag '(#\Space))
  28.                          (test #'(lambda (ch)
  29.                                    (not (member ch char-bag 
  30.                                                 :test 'char=))))
  31.                          (post-process 'identity))
  32.   (let ((pos (position-if test string :start start)))
  33.     (if pos 
  34.       (list-from-string* string :start  pos :char-bag char-bag
  35.                          :test test :post-process post-process)
  36.       nil)))
  37.  
  38. (defun list-from-string* (string
  39.                           &key 
  40.                           (start 0) 
  41.                           (char-bag '(#\Space))
  42.                           (test #'(lambda (ch)
  43.                                     (not (member ch char-bag :test 'char=))))
  44.                           (post-process 'identity))
  45.   (let* ((pos (position-if-not test string :start start))
  46.          (new-pos (if pos (position-if test string :start pos) nil)))
  47.     (cond
  48.      ((and pos new-pos)
  49.       (cons (funcall post-process (subseq string start pos))
  50.             (list-from-string* string :start new-pos :char-bag char-bag
  51.                                :test test :post-process post-process)))
  52.      (pos (list (funcall post-process (subseq string start pos))))
  53.      
  54.      (t (list (funcall post-process (subseq string start)))))))
  55.  
  56.  
  57. (defmethod string->symbol-list ((s string) &optional (package *package*))
  58.   (list-from-string s :post-process
  59.                     #'(lambda (str) 
  60.                         (intern (nstring-upcase str) package))
  61.                     :test 'alphanumericp))
  62.  
  63.  
  64.  
  65. #|
  66.  
  67.  
  68. (list-from-string  "chris dan ski elaine nick") --> 
  69.   ("chris" "dan" "ski" "elaine" "nick")
  70.  
  71. (list-from-string  "chris dan ski elaine nick" 
  72.                    :post-process 'nstring-capitalize) -->
  73.   ("Chris" "Dan" "Ski" "Elaine" "Nick")
  74.  
  75. (list-from-string "chris! dan! ski! elaine! nick!"
  76.                   :char-bag '(#\Space #\!)) -->
  77.   ("chris" "dan" "ski" "elaine" "nick")
  78.  
  79. (string->symbol-list "chris dan ski elaine nick")-->
  80.  (CHRIS DAN SKI ELAINE NICK)
  81.  
  82.  
  83. |#
  84.