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.
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.
(defun list-splitter (split-obj in-list)
(let ((position-split (position split-obj in-list)))
(list
(subseq in-list 0 position-split)
(list (nth position-split in-list))
(subseq in-list (1+ position-split)))))
(list-splitter 'a '(1 3 4 5 0 6 A 2 4 7 8))
(list-splitter 8 '(1 3 4 5 0 6 A 2 4 7 8))
(list-splitter 1 '(1 3 4 5 0 6 A 2 4 7 8))
Carl Taylor
rich wrote:
> 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
(let ((list '(1 3 4 5 0 6 A 2 4 7 8)))
(values (subseq list 0 6)
(subseq list 6 7)
(subseq list 7)))
--
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
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.
>
Geoffrey King <··············@optushome.com.au> wrote:
+---------------
| My (newbie) go at this is:
| (defun tokenize (token seq &optional result) ...)
| ...
| (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))
+---------------
Also see <http://www.cliki.net/SPLIT-SEQUENCE>, which can be used
in similar ways, though by default it indicates the results slightly
differently [including a second return value for the index where
processing stopped]:
> (require :split-sequence)
; Loading #p"/u/lisp/contrib/cclan/split-sequence/split-sequence.x86f".
T
> (use-package :split-sequence)
T
> (split-sequence 'A '(1 2 3 A 5 6 A 8 A 10 11))
((1 2 3) (5 6) (8) (10 11))
11
> (split-sequence 'A '(1 A))
((1) NIL)
2
> (split-sequence 'A '(A 1))
(NIL (1))
2
> (split-sequence 'A '(1 2 3))
((1 2 3))
3
>
To get the same results as yours in the 2nd & 3rd test case,
one would use the ":REMOVE-EMPTY-SUBSEQS T" option:
> (split-sequence 'A '(1 A) :remove-empty-subseqs t)
((1))
2
> (split-sequence 'A '(A 1) :remove-empty-subseqs t)
((1))
2
>
-Rob
-----
Rob Warnock <····@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607