Subject: Re: is there a lisp function as follows?

From: [email-masked] (Erann Gat)
Date: Thu, 31 Aug 95 11:41:33 PDT

(defun split (item l)
  
  "Returns two values - the list l up to the first occurence of item, and
   the rest of l after and including item.  Split is destructive."
  
  (let ( (rest (member item l)) )
    (setf (nthcdr (- (length l) (length rest)) l) nil)
    (values l rest)))

? (setf l '(1 2 3 4 5))
(1 2 3 4 5)
? (split 3 l)
(1 2)
(3 4 5)
? l
(1 2)
? (setf l '(1 2 3 4 5))
(1 2 3 4 5)
? (split 7 l)
(1 2 3 4 5)
NIL
? 

Erann Gat
[email-masked]

---

From: [email-masked] (Bruno Haible)
Date: Thu, 31 Aug 95 21:09:46 +0200

Dan Stanger <[email-masked]> writes:
> i have a list for example f '(1 2 3 4 5)
> and i want to split it into 2 parts on a element for example
> (split 3 f) would return 2 values the list (1 2) and (3 4 5)

A non-destructive split is this:

(defun split (item l)
  (let ((rest (member item l)))
    (values (ldiff l rest) rest)
) )

? (setf l '(1 2 3 4 5))
(1 2 3 4 5)
? (split 3 l)
(1 2) ;
(3 4 5)
? l
(1 2 3 4 5)

                  Bruno

PS: Despite the appearance, this is CLISP. I just modified the prompt:
(defun prompt-string3 () "? ")