Sujet : Re: Use of format or pprint
De : No_spamming (at) *nospam* noWhere_7073.org (B. Pym)
Groupes : comp.lang.lispDate : 02. Jul 2024, 20:44:00
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v61hpf$1ogjd$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
Here are some functions I've written to convert strings into
lists of various sorts. Examples follow at the end.
(defun list-from-string (string
&key
(start 0)
(char-bag '(#\Space))
(test #'(lambda (ch)
(not (member ch char-bag
:test 'char=))))
(post-process 'identity))
(let ((pos (position-if test string :start start)))
(if pos
(list-from-string* string :start pos :char-bag char-bag
:test test :post-process post-process)
nil)))
(defun list-from-string* (string
&key
(start 0)
(char-bag '(#\Space))
(test #'(lambda (ch)
(not (member ch char-bag :test 'char=))))
(post-process 'identity))
(let* ((pos (position-if-not test string :start start))
(new-pos (if pos (position-if test string :start pos) nil)))
(cond
((and pos new-pos)
(cons (funcall post-process (subseq string start pos))
(list-from-string* string :start new-pos :char-bag char-bag
:test test :post-process post-process)))
(pos (list (funcall post-process (subseq string start pos))))
(t (list (funcall post-process (subseq string start)))))))
....
(list-from-string "chris! dan! ski! elaine! nick!"
:char-bag '(#\Space #\!)) -->
("chris" "dan" "ski" "elaine" "nick")
Gauche Scheme
(use srfi-13)
(use srfi-14)
(string-tokenize
" chris!..?..!dan!ski!;elaine! nick! "
char-set:letter)
===>
("chris" "dan" "ski" "elaine" "nick")