Message from discussion
how to split by half?
Path: g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx02.iad01.newshosting.com!newshosting.com!69.16.185.16.MISMATCH!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!spln!extra.newsguy.com!newsp.newsguy.com!enews2
From: "WJ" <w_a_x_...@yahoo.com>
Newsgroups: comp.lang.lisp
Subject: Re: how to split by half?
Date: 6 Mar 2011 22:12:15 GMT
Organization: NewsGuy - Unlimited Usenet $19.95
Lines: 33
Message-ID: <il10rv02ge9@enews2.newsguy.com>
References: <8b5c9d8f-c6ca-4467-99a0-3974feec01d6@35g2000prb.googlegroups.com> <8m6jc2F6guU1@mid.individual.net>
NNTP-Posting-Host: p9a6f6e6f06efd9ab412fa2c985bd942ceb004229a3a98c074a978de224240421.newsdawg.com
User-Agent: XanaNews/1.18.1.6
X-Antivirus: avast! (VPS 110306-0, 03/06/2011), Outbound message
X-Antivirus-Status: Clean
Tamas K Papp 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))))
>
> 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.
>
Guile Scheme:
(define (split-in-half seq)
(let ((n (ceiling (/ (length seq) 2))))
(split-at seq n)))