Re: dividing lists...tokenize



My (newbie) go at this is:

(defun tokenize (token seq &optional result)
(let ((p (position token seq)))
(cond
((null seq)
result)
((null p)
(append result (list seq)))
(T
(tokenize token
(subseq seq (1+ p))
(append result
(if (null (subseq seq 0 p))
nil
(list (subseq seq 0 p)))))))))

(tokenize 'A '(1 2 3 A 5 6 A 8 A 10 11))
((1 2 3) (5 6) (8) (10 11))

(tokenize 'A '(1 A))
((1))

(tokenize 'A '(A 1))
((1))

(tokenize 'A '(1 2 3))
((1 2 3))

Don't really like the two (subseq seq 0 p). Is that evaluated twice?

rich wrote:
hi,
can anybody help m ewith dividing the given list into 3 parts as :
the list i have is
(1 3 4 5 0 6 A 2 4 7 8)
I have to divide this in 3 list as (1 3 4 5 0 6) (A) (2 4 78)
i.e lists to the left and right of symbol A
thanks.

.



Relevant Pages

  • Re: Constants for catch return values?
    ... Where the idea of a [tokenize] might come in handy, ... Especially if it has a few "built in" lists ... of tokens, ... But I think the use of coolness as ...
    (comp.lang.tcl)
  • Re: [PATCH] Rewrite proc seq operations via seq_list_xxx ones
    ... You might want to try implementing this patch on top of the sorted seq ... It deals with a race between proc file read ... AFAIS there's no race - everything is protected with the mutex. ... And yes, all proc files using lists are affected, but changing every ...
    (Linux-Kernel)
  • Re: Constants for catch return values?
    ... and adding a new option to change the behaviour of an existing command is less clean than simply making a new command, ... Especially if it has a few "built in" lists of tokens, ... Something that would end up looking like [tokenize -catch value]. ...
    (comp.lang.tcl)
  • Re: multirember&co
    ... lists from a given one, at the end it applies the generic given fun ... def multiremberandco1(el, seq, fun): ...
    (comp.lang.python)
  • Re: 2 sets of autonum in a document, is this possible
    ... A good place to start would be the "Field codes: Seq field" Help ... Suzanne S. Barnhill ... autonumbered styles from different lists. ...
    (microsoft.public.word.docmanagement)