Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to split by half?

1,043 views
Skip to first unread message

Steve

unread,
Dec 7, 2010, 6:10:11 AM12/7/10
to
I'm learning CLISP, and i would to know if there's a function (or how
to defun'e) that given a sequence (list, array, string...) split it by
half+1, Something like this;

(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,

Tamas K Papp

unread,
Dec 7, 2010, 6:16:18 AM12/7/10
to

(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

Paul Rubin

unread,
Dec 7, 2010, 6:25:33 AM12/7/10
to
Steve <steve.g...@gmail.com> writes:
> I'm learning CLISP, and i would to know if there's a function (or
> how to defun'e) that given a sequence (list, array, string...) split
> it by half+1, Something like this;

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

Steve

unread,
Dec 7, 2010, 6:42:45 AM12/7/10
to
Thanks a lot, both Tamas and Paul!

On 7 dic, 12:25, Paul Rubin <no.em...@nospam.invalid> wrote:

Tamas K Papp

unread,
Dec 7, 2010, 6:51:17 AM12/7/10
to

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

w_a_x_man

unread,
Dec 10, 2010, 2:20:30 AM12/10/10
to
On Dec 7, 5:16 am, Tamas K Papp <tkp...@gmail.com> wrote:
> On Tue, 07 Dec 2010 03:10:11 -0800, Steve wrote:
> > I'm learning CLISP, and i would to know if there's a function (or how to
> > defun'e) that given a sequence (list, array, string...) split it by
> > half+1, Something like this;
>
> > (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.
>
> (defun split-in-half (sequence)
>   (let ((mid (ceiling (length sequence) 2)))
>     (values (subseq sequence 0 mid)
>             (subseq sequence mid nil))))

nil isn't needed here.

Unknown

unread,
Feb 8, 2011, 10:36:29 AM2/8/11
to
Tamas K Papp wrote:


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)]

Thierry Pirot

unread,
Feb 8, 2011, 4:30:16 PM2/8/11
to
William James writes:
> Tamas K Papp wrote:

>> Steve wrote:
>> > given a sequence (list, array, string...) split it by half+1,
[...]

>> (defun split-in-half (sequence)
>> (let ((mid (ceiling (length sequence) 2)))
>> (values (subseq sequence 0 mid)
>> (subseq sequence mid nil))))
[...]

> (defn half-split[coll] (split-at (/ (count coll) 2) coll))

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 ((()()`(,(()))))(()))

WJ

unread,
Mar 6, 2011, 5:12:15 PM3/6/11
to
Tamas K Papp wrote:

Guile Scheme:

(define (split-in-half seq)
(let ((n (ceiling (/ (length seq) 2))))
(split-at seq n)))

Rainer Joswig

unread,
Mar 6, 2011, 5:32:07 PM3/6/11
to

(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))

Rainer Joswig

unread,
Mar 6, 2011, 5:58:19 PM3/6/11
to
On 6 Mrz., 23:12, "WJ" <w_a_x_...@yahoo.com> wrote:

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))

Rainer Joswig

unread,
Mar 6, 2011, 6:03:17 PM3/6/11
to
On 6 Mrz., 23:12, "WJ" <w_a_x_...@yahoo.com> wrote:

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))

Pascal Costanza

unread,
Mar 7, 2011, 1:19:11 AM3/7/11
to

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/

Marco Antoniotti

unread,
Mar 7, 2011, 3:17:25 AM3/7/11
to

Very cool.

--
MA

0 new messages