Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
processing a sequence
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  15 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Scott  
View profile  
 More options Dec 7 2008, 7:08 pm
Newsgroups: comp.lang.lisp
From: Scott <jsam...@gmail.com>
Date: Sun, 7 Dec 2008 16:08:54 -0800 (PST)
Local: Sun, Dec 7 2008 7:08 pm
Subject: processing a sequence
Hi All,

I am trying to process a sequence like:

(0 1 2 3 0 1 2 3 0 1 2 3)

and produce:

((0 1 2 3)(0 1 2 3)(0 1 2 3))

i.e. create sub-sequences beginning whenever the value decreases.

However, I am not having much luck (being pretty new to LISP). My
attempt was to use a loop with two collect statements, collecting each
number into one until its value decreases compared to the last one,
which then gets collected into another, then reset. Something like
this:

(loop
  for num in seq
  when (and (not (null subseq)) (<= num (car (last subseq)))
    collect subseq into answer and
    do (setf subseq nil)
  collect num into subseq
  finally (return answer))

But, subseq never seems to get reset, and it seems that there are
references at work as the return value contains multiple copies of the
original sequence.

Would someone kindly be able to offer some advice or a nudge in the
right direction?

Best,
Scott


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
joswig@corporate-world.li sp.de  
View profile  
 More options Dec 7 2008, 7:49 pm
Newsgroups: comp.lang.lisp
From: "jos...@corporate-world.lisp.de" <jos...@corporate-world.lisp.de>
Date: Sun, 7 Dec 2008 16:49:21 -0800 (PST)
Local: Sun, Dec 7 2008 7:49 pm
Subject: Re: processing a sequence
On Dec 8, 1:08 am, Scott <jsam...@gmail.com> wrote:

(defun collect-sublists (list)
  (flet ((collect-sublist ()
           (loop for element = (pop list)
                 while element collect element
                 while (and list
                            (<= element (first list))))))
    (loop for sublist = (collect-sublist)
          while sublist collect sublist)))

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
André Thieme  
View profile  
 More options Dec 7 2008, 8:05 pm
Newsgroups: comp.lang.lisp
From: André Thieme <address.good.until.2008.oct...@justmail.de>
Date: Mon, 08 Dec 2008 02:05:46 +0100
Local: Sun, Dec 7 2008 8:05 pm
Subject: Re: processing a sequence
jos...@corporate-world.lisp.de schrieb:

> (defun collect-sublists (list)
>   (flet ((collect-sublist ()
>            (loop for element = (pop list)
>                  while element collect element
>                  while (and list
>                             (<= element (first list))))))
>     (loop for sublist = (collect-sublist)
>           while sublist collect sublist)))

Nearly 15 minutes passed since you posted your solution, and William
James still didn’t show his Ruby one-liner that outperforms the Lisp
solution by a factor of 20 (speedwise). ;-)

André
--


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Scott  
View profile  
 More options Dec 7 2008, 9:47 pm
Newsgroups: comp.lang.lisp
From: Scott <jsam...@gmail.com>
Date: Sun, 7 Dec 2008 18:47:10 -0800 (PST)
Local: Sun, Dec 7 2008 9:47 pm
Subject: Re: processing a sequence
On Dec 7, 7:49 pm, "jos...@corporate-world.lisp.de" <jos...@corporate-

world.lisp.de> wrote:
> (defun collect-sublists (list)
>   (flet ((collect-sublist ()
>            (loop for element = (pop list)
>                  while element collect element
>                  while (and list
>                             (<= element (first list))))))
>     (loop for sublist = (collect-sublist)
>           while sublist collect sublist)))

Thanks for the quick response! This does exactly what I need, although
I will certainly have to look at it closely to see exactly how.

Best,
Scott


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
anonymous.c.lis...@gmail.com  
View profile  
(1 user)  More options Dec 7 2008, 10:18 pm
Newsgroups: comp.lang.lisp
From: anonymous.c.lis...@gmail.com
Date: Sun, 7 Dec 2008 19:18:50 -0800 (PST)
Local: Sun, Dec 7 2008 10:18 pm
Subject: Re: processing a sequence
On Dec 7, 9:47 pm, Scott <jsam...@gmail.com> wrote:

Here is a great function from "On Lisp"

(defun group (source n)
  (if (zerop n) (error "zero length"))
  (labels ((rec (source acc)
                (let ((rest (nthcdr n source)))
                  (if (consp rest)
                      (rec rest (cons (subseq source 0 n) acc))
                    (nreverse (cons source acc))))))
    (if source (rec source nil) nil)))

http://www.bookshelf.jp//texi/onlisp/onlisp_5.html
------------

I did it by copy paste, that's 0 lines of code :)
Is one of those functions I keep in my utilities.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kenny  
View profile  
(1 user)  More options Dec 8 2008, 12:24 am
Newsgroups: comp.lang.lisp
From: Kenny <kentil...@gmail.com>
Date: Mon, 08 Dec 2008 00:24:27 -0500
Local: Mon, Dec 8 2008 12:24 am
Subject: Re: processing a sequence

Nonsense! Your request was:

> Would someone kindly be able to offer some advice or a nudge in the
> right direction?

You received an alternative solution that did not identify why your
solution was not working. No advice, no nudge.

As Lao Tzu might have said, "The subseq you can setf is not the true
subseq".

hth,kzo


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Mizrahi  
View profile  
 More options Dec 8 2008, 12:32 pm
Newsgroups: comp.lang.lisp
From: "Alex Mizrahi" <udode...@users.sourceforge.net>
Date: Mon, 8 Dec 2008 19:32:44 +0200
Local: Mon, Dec 8 2008 12:32 pm
Subject: Re: processing a sequence
 acl> Here is a great function from "On Lisp"

i dunno if this one is faster (it might be because it uses
CL library functions, but theoretically it won't matter for "sufficiently
smart compiler"), but Rainer's one is at least order of magnitude easier
to read/understand, and algorithmically it is same O(N).

 acl> (defun group (source n)
 acl>   (if (zerop n) (error "zero length"))
 acl>   (labels ((rec (source acc)
 acl>   (let ((rest (nthcdr n source)))
 acl>     (if (consp rest)
 acl>         (rec rest (cons (subseq source 0 n) acc))
 acl>       (nreverse (cons source acc))))))
 acl>     (if source (rec source nil) nil)))


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
anonymous.c.lis...@gmail.com  
View profile  
 More options Dec 8 2008, 1:45 pm
Newsgroups: comp.lang.lisp
From: anonymous.c.lis...@gmail.com
Date: Mon, 8 Dec 2008 10:45:37 -0800 (PST)
Local: Mon, Dec 8 2008 1:45 pm
Subject: Re: processing a sequence
On Dec 8, 12:32 pm, "Alex Mizrahi" <udode...@users.sourceforge.net>
wrote:

> Rainer's one is at least order of magnitude easier
> to read/understand, and algorithmically it is same O(N).

That is true, although I was kind of hoping he might open the link and
keep reading after not understanding the function (Rainer's is in
there somewhere, I believe). :-P

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dimiter malkia Stanev  
View profile  
 More options Dec 8 2008, 3:24 pm
Newsgroups: comp.lang.lisp
From: "Dimiter \"malkia\" Stanev" <mal...@mac.com>
Date: Mon, 08 Dec 2008 12:24:33 -0800
Local: Mon, Dec 8 2008 3:24 pm
Subject: Re: processing a sequence

Chubby Ruby,
My Dear Hubby,
I love you dear,
But can't you hear,
I'm talking lisp,
... takin' all the risk.

Oh, chubby rubby!
Badooby-booby!


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
William James  
View profile  
(1 user)  More options Dec 10 2008, 5:21 am
Newsgroups: comp.lang.lisp
From: "William James" <w_a_x_...@yahoo.com>
Date: Wed, 10 Dec 2008 11:21:17 +0100 (CET)
Local: Wed, Dec 10 2008 5:21 am
Subject: Re: processing a sequence

André Thieme wrote:
> jos...@corporate-world.lisp.de schrieb:

> > (defun collect-sublists (list)
> >  (flet ((collect-sublist ()
> >           (loop for element = (pop list)
> >                 while element collect element
> >                 while (and list
> >                            (<= element (first list))))))
> >    (loop for sublist = (collect-sublist)
> >          while sublist collect sublist)))

"We don't need no stinkin' loops!"

Ruby:

list = [0,1,2,3] * 3

def collect_sublists list, prev=nil, accum=[[]]
  return accum if not element = list.first
  if prev and element < prev
    accum << [element]
  else
    accum[-1] << element
  end
  collect_sublists( list[1..-1], element, accum )
end

p collect_sublists( list )

# This could be slower if getting the last element in a list
# is expensive.

def collect_sublists_2 list, accum=[[]]
  return accum if not element = list.first
  if accum != [[]] and accum[-1][-1] > element
    accum << [element]
  else
    accum[-1] << element
  end
  collect_sublists_2( list[1..-1], accum )
end

> Nearly 15 minutes passed since you posted your solution, and William
> James still didn’t show his Ruby one-liner that outperforms the Lisp
> solution by a factor of 20 (speedwise). ;-)

> André

Ruby is about the slowest "scripting language".
JavaScript is faster.

SpiderMonkey and jslibs:

LoadModule('jsstd')

list = [0,1,2,3,0,1,2,3,1,2,3]

function collect_groups( list, prev, accum )
{ accum = accum || [[]]
  var element = list[0]
  if ( typeof element == "undefined" )
    return accum
  if (prev && element < prev)
    accum.push( [element] )
  else
    accum.slice(-1)[0].push( element )
  return collect_groups( list.slice(1), element, accum )

}

Print( collect_groups(list).toSource(), '\n')

--- output ---
[[0, 1, 2, 3], [0, 1, 2, 3], [1, 2, 3]]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
joswig@corporate-world.li sp.de  
View profile  
 More options Dec 10 2008, 5:42 am
Newsgroups: comp.lang.lisp
From: "jos...@corporate-world.lisp.de" <jos...@corporate-world.lisp.de>
Date: Wed, 10 Dec 2008 02:42:30 -0800 (PST)
Local: Wed, Dec 10 2008 5:42 am
Subject: Re: processing a sequence
On 10 Dez., 11:21, "William James" <w_a_x_...@yahoo.com> wrote:

 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1,
2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0,
1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3,
0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]
>> p collect_sublists( list )

SystemStackError: stack level too deep
        from (irb):8:in `collect_sublists'
        from (irb):8:in `collect_sublists'
        from (irb):11
        from :0


Oops, you don't need loops?

You need Lisp.

) (1 2 3 4) (1 2 3 4) (1 2 3 4) (1 2 3 4) (1 2 3 4) (1 2 3 4) (1 2 3
4) (1 2 3 4) (1 2 3 4) (1 2 3 4) (1 2 3 4) (1 2 3 4) (1 2 3 4) (1 2 3
4) (1 2 3 4) (1 2 3 4) (1 2 3 4) (1 2 3 4))

CL-USER 3 >


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
William James  
View profile  
 More options Dec 10 2008, 6:01 am
Newsgroups: comp.lang.lisp
From: "William James" <w_a_x_...@yahoo.com>
Date: Wed, 10 Dec 2008 12:01:17 +0100 (CET)
Local: Wed, Dec 10 2008 6:01 am
Subject: Re: processing a sequence

That's extremely prolix, convoluted, and hideous.
If that's the best that Paul Graham can do ...

Ruby:

def group source, n
  acc = []
  0.step(source.size - 1, n){|i|
    acc << source[i,n] }
  acc
end

p group( %w(a b c d e f g), 2)

--- output ---
[["a", "b"], ["c", "d"], ["e", "f"], ["g"]]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
William James  
View profile  
 More options Dec 10 2008, 12:04 pm
Newsgroups: comp.lang.lisp
From: "William James" <w_a_x_...@yahoo.com>
Date: Wed, 10 Dec 2008 18:04:41 +0100 (CET)
Local: Wed, Dec 10 2008 12:04 pm
Subject: Re: processing a sequence

Correct.  You are learning.

> You need Lisp.

> ) (1 2 3 4) (1 2 3 4) (1 2 3 4) (1 2 3 4) (1 2 3 4) (1 2 3 4) (1 2 3
> 4) (1 2 3 4) (1 2 3 4) (1 2 3 4) (1 2 3 4) (1 2 3 4) (1 2 3 4) (1 2 3
> 4) (1 2 3 4) (1 2 3 4) (1 2 3 4) (1 2 3 4))

> CL-USER 3 >

You need Ruby, one line of which does the work of 8 lines of
COBOL Lisp.

# inject is reduce.

list = [2,3,4,5] * 9999

clump=proc{|a| c=[[a[0]]]; a.inject{|p,x| x<p ? c<<[x] : c[-1]<<x;x};c}

p clump[ list ]

....
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5],
[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5]]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
William James  
View profile  
 More options Dec 10 2008, 12:32 pm
Newsgroups: comp.lang.lisp
From: "William James" <w_a_x_...@yahoo.com>
Date: Wed, 10 Dec 2008 18:32:51 +0100 (CET)
Local: Wed, Dec 10 2008 12:32 pm
Subject: Re: processing a sequence

JavaScript (SpiderMonkey):

function clump(a){
  var c=[[a[0]]]
  a.reduce(function(p,x){x<p ? c.push([x]) : c.slice(-1)[0].push(x)
    return x})
  return c }


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rainer Joswig  
View profile  
 More options Dec 10 2008, 2:11 pm
Newsgroups: comp.lang.lisp
From: Rainer Joswig <jos...@lisp.de>
Date: Wed, 10 Dec 2008 20:11:18 +0100
Local: Wed, Dec 10 2008 2:11 pm
Subject: Re: processing a sequence
In article <ghoug3$j4...@aioe.org>,
 "William James" <w_a_x_...@yahoo.com> wrote:

(define-modify-macro b! (&rest args) nconc) (defun clump (l &aux r) (reduce (lambda (a b) (if (> b a) (b! (car (last r)) (list b)) (b! r (list (list b)))) b) l :initial-value (car l)) r)

--
http://lispm.dyndns.org/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google