(split-by-half '(a b c d e f g))
must returns:
(a b c d)
then assign the half+1 part to a list and the other half to another
list.
thnks!
Steve,
(defun split-in-half (sequence)
(let ((mid (ceiling (length sequence) 2)))
(values (subseq sequence 0 mid)
(subseq sequence mid nil))))
Returns the two sequences as two values. See MULTIPLE-VALUE-BIND on
how to make use of both returned values. If you want a list of lists,
replace VALUES by LIST.
Best,
Tamas
It's generally better to figure out these things for yourself.
Do you know about the SUBSEQ function?
http://www.lispworks.com/documentation/HyperSpec/Body/f_subseq.htm
On 7 dic, 12:25, Paul Rubin <no.em...@nospam.invalid> wrote:
Some further comments: I think that it is unlikely that you want to
"learn" CLISP --- that is a particular implementation of Common Lisp,
which is a language with a standard. It is more likely that you want
to learn Common Lisp.
Peter Seibel's Practical Common Lisp is probably the best book for
that purpose, it is also available online. It is best read in a
linear fashion at first, but Chapter 11 on collections should teach
you a lot on sequence manipulation.
Hope this helps,
Tamas
nil isn't needed here.
Clojure:
user=> (defn half-split[coll] (split-at (/ (count coll) 2) coll))
#'user/half-split
user=> (half-split (range 9))
[(0 1 2 3 4) (5 6 7 8)]
C.N. fact :
Chuck Norris is a lisper,
he can split any singleton,
or even the void. ;-)
--
Take it Easy Don't Hurry Be Happy
Thierry
(labels ((()()`(,(()))))(()))
Guile Scheme:
(define (split-in-half seq)
(let ((n (ceiling (/ (length seq) 2))))
(split-at seq n)))
(defun split-at (seq &optional (n (ceiling (length seq) 2)))
(values (subseq seq 0 n) (subseq seq n)))
CL-USER 120 > (split-at (iota 9))
Common Lisp:
(defun split-at (list &optional (n (ceiling (length list) 2)))
(values (loop repeat n collect (pop list))
list))
CL-USER 128 > (split-at (iota 9))
Common Lisp:
without counting:
(defun split-half (list)
(values (loop for nil in list by #'cddr collect (pop list))
list))
CL-USER 156 > (split-half (iota 9))
Neat.
--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
Very cool.
--
MA