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

toy list processing problem: collect similar terms

43 views
Skip to first unread message

Xah Lee

unread,
Sep 26, 2010, 12:05:13 AM9/26/10
to
here's a interesting toy list processing problem.

I have a list of lists, where each sublist is labelled by
a number. I need to collect together the contents of all sublists
sharing
the same label. So if I have the list

((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
r) (5 s t))

where the first element of each sublist is the label, I need to
produce:

output:
((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))

a Mathematica solution is here:
http://xahlee.org/UnixResource_dir/writ/notations_mma.html

R5RS Scheme lisp solution:
http://xahlee.org/UnixResource_dir/writ/Sourav_Mukherjee_sourav.work_gmail.scm
by Sourav Mukherjee

also, a Common Lisp solution can be found here:
http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/5d1ded8824bc750b?

anyone care to give a solution in Python, Perl, javascript, or other
lang? am guessing the scheme solution can be much improved... perhaps
using some lib but that seems to show scheme is pretty weak if the lib
is non-standard.

Xah ∑ xahlee.org

Alexander Burger

unread,
Sep 26, 2010, 1:29:20 AM9/26/10
to
In PicoLisp:

(mapcar
'((X) (apply conc (cdr X)))
(group List) )

Cheers,
- Alex

livibetter

unread,
Sep 26, 2010, 3:47:23 AM9/26/10
to
Here is mine for Python:

l = [[0, 'a', 'b'], [1, 'c', 'd'], [2, 'e', 'f'], [3, 'g', 'h'], [1,
'i', 'j'], [2, 'k', 'l'], [4, 'm', 'n'], [2, 'o', 'p'], [4, 'q', 'r'],
[5, 's', 't']]
d = {}
for idx, items in [(e[0], e[1:]) for e in l]: d[idx] = d[idx] + items
if idx in d else items
print d.values()

Output:
[['a', 'b'], ['c', 'd', 'i', 'j'], ['e', 'f', 'k', 'l', 'o', 'p'],
['g', 'h'], ['m', 'n', 'q', 'r'], ['s', 't']]

Arnaud Delobelle

unread,
Sep 26, 2010, 4:29:53 AM9/26/10
to

from itertools import groupby
from operator import itemgetter

l = [[0, 'a', 'b'], [1, 'c', 'd'], [2, 'e', 'f'], [3, 'g', 'h'], [1,
'i', 'j'], [2, 'k', 'l'], [4, 'm', 'n'], [2, 'o', 'p'], [4, 'q',
'r'],
[5, 's', 't']]

[
[x for g in gs for x in g[1:]]
for _, gs in groupby(sorted(l), itemgetter(0))
]

--
Arnaud

Dr.Ruud

unread,
Sep 26, 2010, 5:41:23 AM9/26/10
to
On 2010-09-26 06:05, Xah Lee wrote:

> I have a list of lists, where each sublist is labelled by
> a number. I need to collect together the contents of all sublists
> sharing the same label. So if I have the list
>
> ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q r) (5 s t))
>
> where the first element of each sublist is the label, I need to
> produce:
>
> output:
> ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))

The input is a string on STDIN,
and the output is a string on STDOUT?


Use a hash:

perl -MData::Dumper -wle '$Data::Dumper::Sortkeys = 1;
my $t = "((0 a b) (1 c d) (2 e f) (3 g h) (1 i j)"
. " (2 k l) (4 m n) (2 o p) (4 q r) (5 s t))";

push @{ $h{ $1 } }, $2 while $t =~ /(\w+)([^)]*)/g; # gist

print Dumper \%h;
'

or an array:

perl -wle '
my $t = "((0 a b) (1 c d) (2 e f) (3 g h) (1 i j)"
. " (2 k l) (4 m n) (2 o p) (4 q r) (5 s t))";

push @{$a[$1]},$2 while $t =~ /(\w+)\s+([^)]*)/g; # gist.1
print "((".join(") (",map join(" ",@$_),@a )."))"; # gist.2
'


Or if the list is not just a string, but a real data structure in the
script:

perl -wle'
my $t = [ [qw/0 a b/], [qw/1 c d/], [qw/2 e f/], [qw/3 g h/],
[qw/1 i j/], [qw/2 k l/], [qw/4 m n/], [qw/2 o p/],
[qw/4 q r/], [qw/5 s t/] ];

push @{ $a[ $_->[0] ] }, [ @$_[ 1, 2 ] ] for @$t; # AoAoA

printf "((%s))\n", join ") (",
map join( " ",
map join( " ", @$_ ), @$_
), @a;
'

Etc.

--
Ruud

Jürgen Exner

unread,
Sep 26, 2010, 9:53:41 AM9/26/10
to
Alexander Burger <a...@software-lab.de> wrote:
>In PicoLisp:

What the f**** does PicoLisp have to with Perl?

jue

Jürgen Exner

unread,
Sep 26, 2010, 9:54:17 AM9/26/10
to
livibetter <livib...@gmail.com> wrote:
>Here is mine for Python:

What the f*** does Python have to do with Perl?

jue

Pascal J. Bourguignon

unread,
Sep 26, 2010, 10:24:46 AM9/26/10
to
Xah Lee <xah...@gmail.com> writes:


> here's a interesting toy list processing problem.
>
> I have a list of lists, where each sublist is labelled by
> a number. I need to collect together the contents of all sublists
> sharing
> the same label. So if I have the list
>
> ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
> r) (5 s t))
>
> where the first element of each sublist is the label, I need to
> produce:
>
> output:
> ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
>
> a Mathematica solution is here:
> http://xahlee.org/UnixResource_dir/writ/notations_mma.html
>
> R5RS Scheme lisp solution:
> http://xahlee.org/UnixResource_dir/writ/Sourav_Mukherjee_sourav.work_gmail.scm
> by Sourav Mukherjee
>
> also, a Common Lisp solution can be found here:
> http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/5d1ded8824bc750b?

It's too complex. Just write:

(let ((list '((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n)
(2 o p) (4 q r) (5 s t))))

(mapcar (lambda (class) (reduce (function append) class :key (function rest)))
(com.informatimago.common-lisp.list:equivalence-classes list :key (function first)))

)

--> ((S T) (Q R M N) (G H) (O P K L E F) (I J C D) (A B))


--
__Pascal Bourguignon__ http://www.informatimago.com/

Alexander Burger

unread,
Sep 26, 2010, 10:54:06 AM9/26/10
to
In comp.lang.lisp Pascal J. Bourguignon <p...@informatimago.com> wrote:
> It's too complex. Just write:
> ...

> (mapcar (lambda (class) (reduce (function append) class :key (function rest)))
> (com.informatimago.common-lisp.list:equivalence-classes list :key (function first)))

Still too complex! ;-)

Sherm Pendley

unread,
Sep 26, 2010, 10:56:07 AM9/26/10
to
Jürgen Exner <jurg...@hotmail.com> writes:

It's Xah. He cross-posts in an attempt to start a language feud.

Please don't feed the troll.

sherm--

--
Sherm Pendley
<http://camelbones.sourceforge.net>
Cocoa Developer

Sherm Pendley

unread,
Sep 26, 2010, 10:56:48 AM9/26/10
to
Jürgen Exner <jurg...@hotmail.com> writes:

Xah is a cross-posting troll. Please don't feed the troll.

Ben Morrow

unread,
Sep 26, 2010, 11:39:56 AM9/26/10
to

Quoth p...@informatimago.com (Pascal J. Bourguignon):

>
> It's too complex. Just write:
>
> (let ((list '((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n)
> (2 o p) (4 q r) (5 s t))))

Unless you're going to talk about Perl, please take clpmisc out of the
xpost.

Ben

John Bokma

unread,
Sep 26, 2010, 2:43:51 PM9/26/10
to
Jürgen Exner <jurg...@hotmail.com> writes:

Clueless fuck. I unsubscribed from comp.lang.perl.misc to avoid the
retards like you and now you come in via the backdoor. If you have a
problem with Xah report him with Google Groups and with his hosting
provider 1&1 like I do. Dreamhost kicked him out that way.

--
John Bokma j3b

Blog: http://johnbokma.com/ Facebook: http://www.facebook.com/j.j.j.bokma
Freelance Perl & Python Development: http://castleamber.com/

Xah Lee

unread,
Sep 26, 2010, 5:03:40 PM9/26/10
to
On Sep 26, 7:56 am, Sherm Pendley <sherm.pend...@gmail.com> wrote:

> Jürgen Exner <jurge...@hotmail.com> writes:
> > Alexander Burger <a...@software-lab.de> wrote:
> >>In PicoLisp:
>
> > What the f**** does PicoLisp have to with Perl?
>
> It's Xah. He cross-posts in an attempt to start a language feud.
>
> Please don't feed the troll.

sorry i disagree. And please don't randomly accuse... I posted the
following in reply to Paul Rubin's very valuable post, to
comp.lang.python only. But since you cross-posted your accusation, and
there are about 3 other posts of similar nature accusing me cross-
posted to all groups, so am posting a response to all groups too.

--------------------------------------------------
Paul,

...

btw, i disagree about your remark on crossposting. For those
interested, you can read in the following:

• 〈Cross-posting &amp; Language Factions〉
http://xahlee.org/Netiquette_dir/cross-post.html

if anyone wants to argue with me about this, there's a my blog link at
the bottom of the page where you can leave a comment. Feel free to use
that.

i'll go over the solutions and post if i have anything interesting to
say. ☺ Possbly will select some to show on my site with credit of
course.

Xah ∑ xahlee.org

Bakul Shah

unread,
Sep 26, 2010, 6:10:46 PM9/26/10
to
On 9/25/10 9:05 PM, Xah Lee wrote:
> here's a interesting toy list processing problem.
>
> I have a list of lists, where each sublist is labelled by
> a number. I need to collect together the contents of all sublists
> sharing
> the same label. So if I have the list
>
> ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
> r) (5 s t))
>
> where the first element of each sublist is the label, I need to
> produce:
>
> output:
> ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
...

> anyone care to give a solution in Python, Perl, javascript, or other
> lang? am guessing the scheme solution can be much improved... perhaps
> using some lib but that seems to show scheme is pretty weak if the lib
> is non-standard.

In Q (from kx.com)[1]:

x:((0; "a"; "b");(1; "c"; "d");(2; "e"; "f");(3; "g"; "h");(1; "i"; "j")
(2; "k"; "l");(4; "m"; "n");(2; "o"; "p");(4; "q"; "r");(5; "s"; "t"))
f:{each [,/] each [each [1 _]] x @ value group x[;0]}
f x

outputs

"ab"
"cdij"
"efklop"
"gh"
"mnqr"
"st"

Note that this is actually a pretty general solution in that *all*
the functions used in it operate on a variety of types.
- The label could be anything, not just an integer.
- What follows a label can be a list of anything.
- Everything, except for = (the group function), has to do with
*shaping* the output in the way you want. It is all basically
cut-n-paste or pulling apart your Lego blocks and constructing
a new toy from them! And most languages are really poor at that.
*This* is why proponents of various languages should pay attention
to such problems.


----
[1] In k3 (an older language from kx.com that you may be able to find
online), x is the same but f is as follows:

f:{,/'1_''x@=x[;0]}

Jürgen Exner

unread,
Sep 26, 2010, 6:16:24 PM9/26/10
to
John Bokma <jo...@castleamber.com> wrote:
>Jürgen Exner <jurg...@hotmail.com> writes:
>
>> livibetter <livib...@gmail.com> wrote:
>>>Here is mine for Python:
>>
>> What the f*** does Python have to do with Perl?
>
>Clueless fuck. I unsubscribed from comp.lang.perl.misc to avoid the
>retards like you and now you come in via the backdoor. If you have a
>problem with Xah report him with Google Groups and with his hosting
>provider 1&1 like I do. Dreamhost kicked him out that way.

I have solved my problems with Xah years ago, that's what killfiles are
for. And I have no idea why you are bringing up Xah in your current
rant.

It was livibetter who without any motivation or reasoning posted Python
code in CLPM. If at least he had asked something like "How can I write
something similar in Perl?" or _ANYTHING_ that would have made this even
remotely meaningful for CLPM, then ok.
But he didn't. He just dumped 7 lines of Python code to CLPM and his
only comment was "Here is mine for Python". Yeah, great comment. Why
would anyone in CLPM possibly care about those 7 lines of Phython code?

jue

jue

Rhodri James

unread,
Sep 26, 2010, 7:19:58 PM9/26/10
to
On Sun, 26 Sep 2010 23:16:24 +0100, Jürgen Exner <jurg...@hotmail.com>
wrote:

> I have solved my problems with Xah years ago, that's what killfiles are
> for. And I have no idea why you are bringing up Xah in your current
> rant.
>
> It was livibetter who without any motivation or reasoning posted Python
> code in CLPM. If at least he had asked something like "How can I write
> something similar in Perl?" or _ANYTHING_ that would have made this even
> remotely meaningful for CLPM, then ok.
> But he didn't. He just dumped 7 lines of Python code to CLPM and his
> only comment was "Here is mine for Python". Yeah, great comment. Why
> would anyone in CLPM possibly care about those 7 lines of Phython code?

Check the Newsgroups line: this is a standard Xah "cross-post to
everywhere in sight" troll. Hence (a) bringing up Xah, since he
originated this thread, and (b) livibetter not thinking to trim the
crossposts. Which you didn't either, I note.

The only downside to killfiles is that you have to pay attention or
you sometimes get bitten by this stuff.

--
Rhodri James *-* Wildebeest Herder to the Masses

Ertugrul Söylemez

unread,
Sep 26, 2010, 7:41:37 PM9/26/10
to
Xah Lee <xah...@gmail.com> wrote:

> here's a interesting toy list processing problem.
>
> I have a list of lists, where each sublist is labelled by a number. I
> need to collect together the contents of all sublists sharing the same
> label. So if I have the list
>
> ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
> r) (5 s t))
>
> where the first element of each sublist is the label, I need to
> produce:
>
> output:
> ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
>

> [...]


>
> anyone care to give a solution in Python, Perl, javascript, or other
> lang? am guessing the scheme solution can be much improved... perhaps
> using some lib but that seems to show scheme is pretty weak if the lib
> is non-standard.

In Haskell the solution looks like this:

import qualified Data.Map as M
import qualified Data.Set as S
import Data.Map (Map)
import Data.Set (Set)

collect :: (Ord a, Ord k) => [Map k (Set a)] -> Map k (Set a)
collect = M.unionsWith S.union


Greets,
Ertugrul


--
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/

Ertugrul Söylemez

unread,
Sep 26, 2010, 7:47:59 PM9/26/10
to
Ertugrul Söylemez <e...@ertes.de> wrote:

> In Haskell the solution looks like this:
>

> [...]

And before anyone starts to rant, I didn't pay attention to where I'm
X-posting this stuff. Sorry for that. But on the other hand you could
say that I'm giving the Perl people (apparently the only people feeling
the urge to rant anyway) the chance to come up with a more elegant
solution. =)

Mirko

unread,
Sep 27, 2010, 11:18:22 AM9/27/10
to
On Sep 26, 12:05 am, Xah Lee <xah...@gmail.com> wrote:

I am hijacking the following post and driving it to Cuba (the Monthy
Python fans will know what I refer to). I want to create a `reduce'-
like function that can handle similar problems.

Xah said:
> here's a interesting toy list processing problem.
>
> I have a list of lists, where each sublist is labelled by
> a number. I need to collect together the contents of all sublists
> sharing
> the same label. So if I have the list
>
> ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
> r) (5 s t))
>
> where the first element of each sublist is the label, I need to
> produce:
>
> output:
> ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
>

> stuffed deleted.

Here is my Common Lisp (and I only care about Common Lisp answers)
attempt to create a `reduce'-like function to handle this kind of a
problem (you will see that I am still struggling with the code and the
documentation).

(defun reduce-tagged (function sequence &key
(key-tag #'first)
(key-datum #'rest))
"Use a binary operation, `function' to combine a sequence of tagged
elements. like-tagged elements are `reduce'd according to `function'
and returned in a list ...

`sequence' is a sequence of tagged elements. reduce-m will reduce
like-tagged-elements.

If `key-tag' is supplied it is used to extract the element tag. If
`key-tag' is not supplied, the function `first' is used.

If `key-datum' is supplied, it is used to extract the element datum.
If `key-datum' is not supplied, the function `rest' is used.

"
(let ((hash (make-hash-table)))
(dolist (datum sequence)
(let ((tag (funcall key-tag datum))
(values (funcall key-datum datum)))
(multiple-value-bind (it present)
(gethash tag hash)
(if present
(setf (gethash tag hash)
(apply function (gethash tag hash) values))
(setf (gethash tag hash) values)))))
(let (result)
(dohash (key value hash)
(push (list key value) result))
result)))

Comments, improvements? I am looking for a general purpose function
like reduce that I
can apply in various situations.

Thanks,

Mirko

Mirko

unread,
Sep 27, 2010, 11:40:45 AM9/27/10
to

Correction: the previous code used a non-portable clisp macro
`dohash' (looks nice, doesn't it?)

Here is the version with maphash:

(defun reduce-tagged (function sequence &key
(key-tag #'first)
(key-datum #'rest))
"Use a binary operation, `function' to combine a sequence of tagged
elements. like-tagged elements are `reduce'd according to `function'

`sequence' is a sequence of tagged elements. reduce-m will reduce
like-tagged-elements.

If `key-tag' is supplied it is used to extract the element tag. If
`key-tag' is not supplied, the function `first' is used.

If `key-datum' is supplied, it is used to extract the element datum.
If `key-datum' is not supplied, the function `rest' is used.

"
(let ((hash (make-hash-table)))
(dolist (datum sequence)
(let ((tag (funcall key-tag datum))
(values (funcall key-datum datum)))
(multiple-value-bind (it present)
(gethash tag hash)

(declare (ignore it))


(if present
(setf (gethash tag hash)
(apply function (gethash tag hash) values))
(setf (gethash tag hash) values)))))
(let (result)

(maphash #'(lambda(key value)


(push (list key value) result))

hash)
result)))

Mirko

Mirko

unread,
Sep 27, 2010, 11:50:39 AM9/27/10
to
On Sep 27, 11:40 am, Mirko <mirko.vuko...@gmail.com> wrote:
> On Sep 27, 11:18 am, Mirko <mirko.vuko...@gmail.com> wrote:
>
>
>
> > On Sep 26, 12:05 am, Xah Lee <xah...@gmail.com> wrote:
>
> > I am hijacking the following post and driving it to Cuba (the Monthy
> > Python fans will know what I refer to).  I want to create a `reduce'-
> > like function that can handle similar problems.
>
> > Xah said:
>
> > > here's a interesting toy list processing problem.
>
> > > I have a list of lists, where each sublist is labelled by
> > > a number. I need to collect together the contents of all sublists
> > > sharing
> > > the same label. So if I have the list
>
> > > ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
> > > r) (5 s t))
>
> > > where the first element of each sublist is the label, I need to
> > > produce:
>
> > > output:
> > > ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
>
> > > stuffed deleted.
>
> > Here is my Common Lisp (and I only care about Common Lisp answers)
> > attempt to create a `reduce'-like function to handle this kind of a
> > problem (you will see that I am still struggling with the code and the
> > documentation).
>
> ... faulty code deleted

Aaand one more fix (apply -> funcall) (This version at least produces
a close
facsimile of the desired output)

(defun reduce-tagged (function sequence &key
(key-tag #'first)
(key-datum #'rest))
"Use a binary operation, `function' to combine a sequence of tagged
elements. like-tagged elements are `reduce'd according to `function'

`sequence' is a sequence of tagged elements. reduce-m will reduce
like-tagged-elements.

If `key-tag' is supplied it is used to extract the element tag. If
`key-tag' is not supplied, the function `first' is used.

If `key-datum' is supplied, it is used to extract the element datum.
If `key-datum' is not supplied, the function `rest' is used.

"
(let ((hash (make-hash-table)))
(dolist (datum sequence)
(let ((tag (funcall key-tag datum))
(values (funcall key-datum datum)))
(multiple-value-bind (it present)
(gethash tag hash)
(declare (ignore it))
(if present
(setf (gethash tag hash)

(funcall function (gethash tag hash) values))

ccc31807

unread,
Sep 27, 2010, 4:43:37 PM9/27/10
to
On Sep 26, 12:05 am, Xah Lee <xah...@gmail.com> wrote:
> here's a interesting toy list processing problem.
>
> I have a list of lists, where each sublist is labelled by
> a number. I need to collect together the contents of all sublists
> sharing
> the same label. So if I have the list
>
> ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
> r) (5 s t))
>
> where the first element of each sublist is the label, I need to
> produce:
>
> output:
> ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))

Here is a solution in Perl -- the verbose version. Please see my note
below.

SCRIPT:
use strict;
use warnings;
my %lists;

while (<DATA>)
{
chomp;
my ($k, @v) = split(/ /, $_);
push(@{$lists{$k}}, @v);
}

foreach my $k (sort keys %lists)
{
print "$k - @{$lists{$k}}\n";
}

exit(0);

__DATA__
0 a b
1 c d
2 e f
3 g h
1 i j
2 k l
4 m n
2 o p
4 q r
5 s t

OUTPUT:
>perl lists.plx
0 - a b
1 - c d i j
2 - e f k l o p
3 - g h
4 - m n q r
5 - s t

NOTE:
I assume that you want an idiomatic solution for the language. I have
therefore converted your data into a typical record oriented
structure. Perlistas don't use parenthesis. If you want a Lispy
solution, use Lisp. Further, Perl was made for exactly this kind of
problem, which is simple data munging, taking some input, transforming
it, and printing it out -- Practical Extraction and Reporting
Language. I know that some Lispers (R.G., are you reading?) will
object to a formulation like this: @{$lists{$k}}, but all this says
(in Perl) is to spit out the value contained in the hash element
$lists{$k} as an array, and is good idiomatic Perl, even if some
Lispers aren't quite up to the task of understanding it.

CC.

Steven D'Aprano

unread,
Sep 27, 2010, 10:34:39 PM9/27/10
to
On Mon, 27 Sep 2010 08:18:22 -0700, Mirko wrote:

> Here is my Common Lisp (and I only care about Common Lisp answers)

Good for you. So why are you spamming other newsgroups with your CL
solution? Not once, but three times.

Replies to /dev/null.

--
Steven

Seebs

unread,
Sep 27, 2010, 10:45:50 PM9/27/10
to
On 2010-09-26, J?rgen Exner <jurg...@hotmail.com> wrote:
> It was livibetter who without any motivation or reasoning posted Python
> code in CLPM.

Not exactly; he posted it in a crossposted thread, which happened to include
CLPM and other groups, including comp.lang.python.

It is quite possible that he didn't know about the crossposting. However,
while I would agree that this would constitute a form of ignorance, I'd think
that, especially with how well some newsreading interfaces hide that detail,
I don't think it's really worth getting angry over.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

John Bokma

unread,
Sep 27, 2010, 11:36:27 PM9/27/10
to
Seebs <usenet...@seebs.net> writes:

> On 2010-09-26, J?rgen Exner <jurg...@hotmail.com> wrote:
>> It was livibetter who without any motivation or reasoning posted Python
>> code in CLPM.
>
> Not exactly; he posted it in a crossposted thread, which happened to include
> CLPM and other groups, including comp.lang.python.
>
> It is quite possible that he didn't know about the crossposting.

Oh, he does. It has been Xah's game for years.

> while I would agree that this would constitute a form of ignorance, I'd think
> that, especially with how well some newsreading interfaces hide that detail,
> I don't think it's really worth getting angry over.

You think wrong. And Jurgen should have known better than to reply several
times (but like too many people in cplm he posts for posting's sake, the
main reason why I don't follow that group anymore).

Xah has been doing this for many years and most of his posts are just
made to drive traffic to his site (hence they are copy paste of articles
on his site + link(s) to his site). It's Usenet abuse, on purpose.

The reason it pisses off people is that nearly weekly it causes a lot of
noise in newsgroups that are really better off without the noise
(IMNSHO).

See my other post on how to deal with this spammer. If you've missed it:
report him for spamming, since that's what he does. It already made him
have to move hosting providers once. While it's not going to stop him,
it will cost him money. See:
http://www.google.com/search?q=site%3Axahlee.org%20bokma

While I am named in that article be assured that I was not the only one
contacting dreamhost (+10 for doing this, btw). Quite some people
contacted me via email that they also talked with Dreamhost. Just keep
reporting this spammer, and maybe 1and1 will kick it out.

Seebs

unread,
Sep 28, 2010, 12:18:38 AM9/28/10
to
On 2010-09-28, John Bokma <jo...@castleamber.com> wrote:
> Seebs <usenet...@seebs.net> writes:
>> On 2010-09-26, J?rgen Exner <jurg...@hotmail.com> wrote:
>>> It was livibetter who without any motivation or reasoning posted Python
>>> code in CLPM.

>> Not exactly; he posted it in a crossposted thread, which happened to include
>> CLPM and other groups, including comp.lang.python.

>> It is quite possible that he didn't know about the crossposting.

> Oh, he does. It has been Xah's game for years.

But did "livibetter" know about it? I wasn't defending Xah, who is indeed
at the very least clueless and disruptive. But I was sort of defending
the poster who was accused of posting Python code in CLPM, because that
poster may not have understood the game.

John Bokma

unread,
Sep 28, 2010, 12:34:22 AM9/28/10
to
Seebs <usenet...@seebs.net> writes:

fup set to poster

> On 2010-09-28, John Bokma <jo...@castleamber.com> wrote:
>> Seebs <usenet...@seebs.net> writes:
>>> On 2010-09-26, J?rgen Exner <jurg...@hotmail.com> wrote:
>>>> It was livibetter who without any motivation or reasoning posted Python
>>>> code in CLPM.
>
>>> Not exactly; he posted it in a crossposted thread, which happened to include
>>> CLPM and other groups, including comp.lang.python.
>
>>> It is quite possible that he didn't know about the crossposting.
>
>> Oh, he does. It has been Xah's game for years.
>
> But did "livibetter" know about it? I wasn't defending Xah, who is indeed
> at the very least clueless and disruptive.

Heh, he's not clueless, the problem is that he knows exactly what he's
doing. And like most spammers, very hard to get rid off.

> But I was sort of defending
> the poster who was accused of posting Python code in CLPM, because that
> poster may not have understood the game.

Ah, clear. Well, the problem is somewhat also in CLPM where people
somehow have to reply to messages like this :-(. And I am sure Xah
laughes his ass off each time it happens.

Xah Lee

unread,
Sep 28, 2010, 1:39:31 PM9/28/10
to
On Sep 27, 9:34 pm, John Bokma <j...@castleamber.com> wrote:

> Seebs <usenet-nos...@seebs.net> writes:
>
> fup set to poster
>
> > On 2010-09-28, John Bokma <j...@castleamber.com> wrote:
> >> Seebs <usenet-nos...@seebs.net> writes:

> >>> On 2010-09-26, J?rgen Exner <jurge...@hotmail.com> wrote:
> >>>> It was livibetter who without any motivation or reasoning posted Python
> >>>> code in CLPM.
>
> >>> Not exactly; he posted it in a crossposted thread, which happened to include
> >>> CLPM and other groups, including comp.lang.python.
>
> >>> It is quite possible that he didn't know about the crossposting.
>
> >> Oh, he does. It has been Xah's game for years.
>
> > But did "livibetter" know about it?  I wasn't defending Xah, who is indeed
> > at the very least clueless and disruptive.
>
> Heh, he's not clueless, the problem is that he knows exactly what he's
> doing. And like most spammers, very hard to get rid off.
>
> > But I was sort of defending
> > the poster who was accused of posting Python code in CLPM, because that
> > poster may not have understood the game.
>
> Ah, clear. Well, the problem is somewhat also in CLPM where people
> somehow have to reply to messages like this :-(. And I am sure Xah
> laughes his ass off each time it happens.

Hi John Bokma,

can you stop this?

doesn't seems fruitful to keep on this.

if you don't like my posts, ignore them? i don't post in
comp.lang.python or comp.lang.perl.misc that often... maybe have done
so 5 times this year.

i visited your home page
http://johnbokma.com/mexit/2010/08/15/
and there are really a lot beautiful photos.

this isn't bribery or something like that. I've been annoyed by you,
of course, but it's not fruitful to keep going on this.

Best,

Xah ∑ xahlee.org

John Bokma

unread,
Sep 28, 2010, 2:13:27 PM9/28/10
to
Xah Lee <xah...@gmail.com> writes:

> can you stop this?

Can you stop crossposting? And if there is really, really a need to
crosspost, can you please set the follow-up to?

> doesn't seems fruitful to keep on this.
>
> if you don't like my posts, ignore them? i don't post in
> comp.lang.python or comp.lang.perl.misc that often... maybe have done
> so 5 times this year.

Which is enough to disrupt those groups for days.

> i visited your home page
> http://johnbokma.com/mexit/2010/08/15/
> and there are really a lot beautiful photos.

Thanks Xah. Like I wrote, your site /does/ have good information, it's
so sad that you somehow think it's necessary to spam Usenet to get
visitors. Or maybe you've another reason, don't know. But it /is/ Usenet
abuse.

> this isn't bribery or something like that. I've been annoyed by you,
> of course, but it's not fruitful to keep going on this.

Well, you annoy me, I annoy you. It's in your hands to make it stop.

My advice is:

1) remove all the excessive swearing from your site. If you have a
point, you don't need it. Your argument(s) without the swearing
should speak for themselves

2) Stop abusing Usenet. Instead focus on writing more good stuff on
your site.

1) & 2) will keep me from linking to your site, ever. And I am sure I am
not alone in this.

w_a_x_man

unread,
Sep 29, 2010, 10:04:26 AM9/29/10
to
On Sep 26, 9:24 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:

> Xah Lee <xah...@gmail.com> writes:
> > here's a interesting toy list processing problem.
>
> > I have a list of lists, where each sublist is labelled by
> > a number. I need to collect together the contents of all sublists
> > sharing
> > the same label. So if I have the list
>
> > ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
> > r) (5 s t))
>
> > where the first element of each sublist is the label, I need to
> > produce:
>
> > output:
> > ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
>
> > a Mathematica solution is here:
> >http://xahlee.org/UnixResource_dir/writ/notations_mma.html
>
> > R5RS Scheme lisp solution:
> >http://xahlee.org/UnixResource_dir/writ/Sourav_Mukherjee_sourav.work_...

> > by Sourav Mukherjee
>
> > also, a Common Lisp solution can be found here:
> >http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/5d1de...

>
> It's too complex. Just write:
>
> (let ((list '((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n)
>               (2 o p) (4 q r) (5 s t))))
>
>   (mapcar (lambda (class) (reduce (function append) class :key (function rest)))
>            (com.informatimago.common-lisp.list:equivalence-classes list :key (function first)))
>
>    )
>
> --> ((S T) (Q R M N) (G H) (O P K L E F) (I J C D) (A B))
>
> --
> __Pascal Bourguignon__                    http://www.informatimago.com/

Ruby:

[[0, 'a', 'b'], [1, 'c', 'd'], [2, 'e', 'f'], [3, 'g', 'h'], [1,
'i', 'j'], [2, 'k', 'l'], [4, 'm', 'n'], [2, 'o', 'p'], [4, 'q', 'r'],
[5, 's', 't']].
group_by{|x| x.first}.values.map{|x| x.map{|y| y[1..-1]}.flatten}

==>[["s", "t"], ["a", "b"], ["c", "d", "i", "j"],
["e", "f", "k", "l", "o", "p"],
["g", "h"], ["m", "n", "q", "r"]]

namekuseijin

unread,
Sep 30, 2010, 8:35:20 AM9/30/10
to

cool, it comes with order all fucked up. This is something I was
criticized for before, though not all that important to most
functional processing. Not the case here, though.

here's a scheme version that is hopefully better than the given one:

(define (dig in)

(if (null? in) '()

(let* ((n (first-of-first in))

(all-n (filter in (lambda (x) (eq? n (first x)))))

(all-but-n (filter in (lambda (x) (not (eq? n (first
x)))))))

(pair

(fold all-n
(lambda (i o) (pair (second i) (pair (third i) o))))

(dig all-but-n)))))


; given these aliases to lisp n00bs

(define pair cons)

(define first car)

(define rest cdr)

(define first-of-first caar)

(define second cadr)

(define third caddr)


; and these well-known functions
(non-tail-recursive for benefit of n00bs)
(define (fold ls f) ; AKA reduce

(if (null? ls) '()

(f (first ls) (fold (rest ls) f))))


(define (filter ls f)

(fold ls (lambda (i o) (if (f i) (pair i o) o))))

; testing
(let ((in '((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n)


(2 o p) (4 q r) (5 s t))))

(display (dig in))
(newline))

namekuseijin

unread,
Sep 30, 2010, 9:32:32 AM9/30/10
to

;frakkin text editor...

s...@netherlands.com

unread,
Oct 6, 2010, 1:52:19 PM10/6/10
to
On Sat, 25 Sep 2010 21:05:13 -0700 (PDT), Xah Lee <xah...@gmail.com> wrote:

>here's a interesting toy list processing problem.
>
>I have a list of lists, where each sublist is labelled by
>a number. I need to collect together the contents of all sublists
>sharing
>the same label. So if I have the list
>
>((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
>r) (5 s t))
>
>where the first element of each sublist is the label, I need to
>produce:
>
>output:
>((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
>

[snip]

>anyone care to give a solution in Python, Perl, javascript, or other
>lang? am guessing the scheme solution can be much improved... perhaps
>using some lib but that seems to show scheme is pretty weak if the lib
>is non-standard.
>

Crossposting to Lisp, Python and Perl because the weird list of lists looks
like Lisp or something else, and you mention other languages so I'm throwing
this out for Perl.

It appears this string you have there is actually list syntax in another language.
If it is, its the job of the language to parse the data out. Why then do you
want to put it into another language form? At runtime, once the data is in variables,
dictated by the syntax, you can do whatever data manipulation you want
(combining arrays, etc..).

So, in the spirit of a preprocessor, given that the text is balanced, with proper closure,
ie: ( (data) (data) ) is ok.
( data (data) ) is not ok.

the below does simple text manipulation, joining like labeled sublists, without going into
the runtime guts of internalizing the data itself. Internally, this is too simple.

-sln
-----------------
Alternate input:
(
(


(0 a b) (1 c d) (2 e f )

)


(3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q r) (5 s t)
)

------------------
use strict;
use warnings;

my $input = <<EOI;


((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q r)
(5 s t))

EOI
my $output = $input;

my $regxout = qr/
( (?: \( \s* [^()]+ \s* \) (\s*) )+ )
/x;


$output =~
s{ $regxout }
{
my ( $list, $format ) = ( $1, $2 );
my ( %hseen,
@order,
$replace
);
while ($list =~ /\(\s* (\S+) \s* (.+?) \s*\)/xsg) {
if ( exists $hseen{$1} ) {
$hseen{$1} .= " $2";
next;
}
push @order, $1;
$hseen{$1} = $2;
}
for my $id (@order) {
$replace .= "($hseen{$id}) ";
}
$replace =~ s/ $//;
$replace . $format
}xeg;

print "Input -\n$input\n";
print "Output -\n$output";

__END__

Input -


((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q r)
(5 s t))

Output -

Thomas A. Russ

unread,
Oct 6, 2010, 3:36:46 PM10/6/10
to
s...@netherlands.com writes:

Limited reply to c.l.l.

> On Sat, 25 Sep 2010 21:05:13 -0700 (PDT), Xah Lee <xah...@gmail.com> wrote:
>
> >here's a interesting toy list processing problem.
> >
> >I have a list of lists, where each sublist is labelled by
> >a number. I need to collect together the contents of all sublists
> >sharing
> >the same label. So if I have the list
> >
> >((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
> >r) (5 s t))
> >
> >where the first element of each sublist is the label, I need to
> >produce:
> >
> >output:
> >((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))

I will assume that you need to preserve the order of the sublist
elements. Or would

((a b) (c d i j) (o p k l e f) (g h) (q r m n) (s t))

also be an acceptable solution? This is mainly an efficiency issue.

Here are a couple of Common Lisp solutions. Running time assumes that
the O(x^2) time for multiple appends is minimal. If the internal order
doesn't matter, then the direction of the append can be reversed with
reduction of the O(x^2) to O(x).

(defun order-keyed-elements (input)
"An array-based solution. Assumes all keys are non-negative
integers. Works best with dense key sequences. O(n)"
(let* ((max-key (loop for (key . nil) in input maximize key))
(array (make-array (1+ max-key) :initial-element nil)))
(loop for (key . values) in input
do (setf (aref array key) (append (aref array key) values)))
(loop for values across array
unless (null values) collect values)))

(defun order-keyed-elements (input)
"A hash-table-based solution. No restriction on key value. O(n log n)"
(let ((ht (make-hash-table))
(output nil))
(loop for (key . values) in input
do (setf (gethash key ht) (append (gethash key ht nil) values)))
(maphash #'(lambda (k v) (push (cons k v) output)) ht)
(mapcar #'rest (sort output #'< :key #'first))))

(defun order-keyed-elements (input)
"A different hash-table-based solution. No restriction on key
value. O(n^2)"
(let ((ht (make-hash-table))
(keys nil))
(loop for (key . values) in input
do (setf (gethash key ht) (append (gethash key ht nil) values))
(pushnew key keys))
(setq keys (sort keys #'<))
(loop for key in keys collect (gethash key ht))))

(defun order-keyed-elements (input)
"A different hash-table-based solution. No restriction on key
value. O(n log n)"
(let ((ht (make-hash-table))
(keys nil))
(loop for (key . values) in input
do (multiple-value-bind (old-values foundp) (gethash key ht nil)
(if foundp
(setf (gethash key ht) (append old-values values))
(setf (gethash key ht) values
keys (cons key keys)))))
(setq keys (sort keys #'<))
(loop for key in keys collect (gethash key ht))))


--
Thomas A. Russ, USC/Information Sciences Institute

s...@netherlands.com

unread,
Oct 7, 2010, 8:44:31 PM10/7/10
to

If not preprocessor, then ...
The too simple, order independent, id independent, Perl approach.

-sln
-----------------

use strict;
use warnings;
use Data::Dump 'dump';

my @inp = ([0,'a','b'],[1,'c','d'],[2,'e','f'],[3,'g','h'],


[1,'i','j'],[2,'k','l'],[4,'m','n'],[2,'o','p'],

[4,'q','r'],[5,'s','t']);

my ($cnt, @outp, %hs) = (0);

for my $ref (@inp) {
$hs{ $$ref[0] } or $hs{ $$ref[0] } = $cnt++;
push @{$outp[ $hs{ $$ref[0] } ] }, @{$ref}[ 1 .. $#{$ref} ];
}

dump @outp;

__END__

(


["a", "b"],
["c", "d", "i", "j"],
["e", "f", "k", "l", "o", "p"],
["g", "h"],

["m", "n", "q", "r"],
["s", "t"],
)

Xah Lee

unread,
Oct 14, 2010, 11:37:03 PM10/14/10
to

On Sep 25, 9:05 pm, Xah Lee <xah...@gmail.com> wrote:
> here's a interesting toy list processing problem.
>
> I have a list of lists, where each sublist is labelled by
> a number. I need to collect together the contents of all sublists
> sharing
> the same label. So if I have the list
>
> ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
> r) (5 s t))
>
> where the first element of each sublist is the label, I need to
> produce:
>
> output:
> ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
> ...

thanks all for many interesting solutions. I've been so busy in past
month on other computing issues and writing and never got around to
look at this thread. I think eventually i will, but for now just made
a link on my page to point to here.

now we have solutions in perl, python, ruby, common lisp, scheme lisp,
mathematica. I myself would also be interested in javascript perhps
i'll write one soon. If someone would go thru all these solution and
make a good summary with consistent format/names of each solution...
that'd be very useful i think. (and will learn a lot, which is how i
find this interesting)

PS here's a good site that does very useful comparisons for those
learning multiple langs.

* 〈Lisp: Common Lisp, Scheme, Clojure, Emacs Lisp〉
http://hyperpolyglot.wikidot.com/lisp
* 〈Scripting Languages: PHP, Perl, Python, Ruby, Smalltalk〉
http://hyperpolyglot.wikidot.com/scripting
* 〈Scripting Languages: Bash, Tcl, Lua, JavaScript, Io〉
http://hyperpolyglot.wikidot.com/small
* 〈Platform Languages: C, C++, Objective C, Java, C#〉
http://hyperpolyglot.wikidot.com/c
* 〈ML: Standard ML, OCaml, F#, Scala, Haskell〉 http://hyperpolyglot.wikidot.com/ml

Xah ∑ http://xahlee.org/

WJ

unread,
Feb 10, 2011, 4:03:01 PM2/10/11
to
Pascal J. Bourguignon wrote:

> Xah Lee <xah...@gmail.com> writes:
>
>
> > here's a interesting toy list processing problem.
> >
> > I have a list of lists, where each sublist is labelled by
> > a number. I need to collect together the contents of all sublists
> > sharing
> > the same label. So if I have the list
> >
> > ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4
> > q r) (5 s t))
> >
> > where the first element of each sublist is the label, I need to
> > produce:
> >
> > output:
> > ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
> >

> > _gmail.scm by Sourav Mukherjee


> >
> > also, a Common Lisp solution can be found here:
> > http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/5d1d

> > ed8824bc750b?


>
> It's too complex. Just write:
>

> (let ((list '((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n)

> (2 o p) (4 q r) (5 s t))))
>
> (mapcar (lambda (class) (reduce (function append) class :key
> (function rest)))
> (com.informatimago.common-lisp.list:equivalence-classes list :key
> (function first)))
>
> )
>
> --> ((S T) (Q R M N) (G H) (O P K L E F) (I J C D) (A B))

Clojure:

(def groups '((0 a b)(1 c d)(2 e f)(3 g h)(1 i j)(2 k l)(4 m n)
(2 o p)(4 q r) (5 s t)))

Using group-by:

(map (fn[[k v]](flatten (map rest v))) (group-by first groups))

Using reduce:

(map #(flatten(rest %)) (reduce (fn[h [k & v]]
(merge-with concat h {k v})) {} groups))

George Mpouras

unread,
Feb 10, 2011, 5:22:59 PM2/10/11
to
Στις 10/2/2011 11:03 μμ, ο/η WJ έγραψε:
> ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4
>> > q r) (5 s t))
>> >


# Here is your 2 lines Perl solution my friend

my $i=0;
my %R;
my $Output;
my $Inpout =
[


[0, 'a', 'b'],
[1, 'c', 'd'],
[2, 'e', 'f'],
[3, 'g', 'h'],
[1, 'i', 'j'],
[2, 'k', 'l'],
[4, 'm', 'n'],
[2, 'o', 'p'],
[4, 'q', 'r'],

[5, 's', 't'],
];

foreach (@{$Inpout}) { push @{$R{$_->[0]}}, @{$_}[1..$#{$_}] }
foreach (sort keys %R) { $Output->[$i++] = $R{$_}}

use Data::Dumper; print Dumper($Output);

George Mpouras

unread,
Feb 10, 2011, 5:24:12 PM2/10/11
to
Στις 10/2/2011 11:03 μμ, ο/η WJ έγραψε:
> (map #(flatten(rest %)) (reduce (fn[h [k& v]]

> (merge-with concat h {k v})) {} groups))
>


# Here is your 2 line solution my friend

George Mpouras

unread,
Feb 10, 2011, 5:26:45 PM2/10/11
to
Στις 10/2/2011 11:03 μμ, ο/η WJ έγραψε:
> (map #(flatten(rest %)) (reduce (fn[h [k& v]]

> (merge-with concat h {k v})) {} groups))
>

my $i=0;

Jürgen Exner

unread,
Feb 10, 2011, 8:34:08 PM2/10/11
to
"WJ" <w_a_...@yahoo.com> wrote:
>Pascal J. Bourguignon wrote:
>
>> Xah Lee <xah...@gmail.com> writes:

+-------------------+ .:\:\:/:/:.
| PLEASE DO NOT | :.:\:\:/:/:.:
| FEED THE TROLLS | :=.' - - '.=:
| | '=(\ 9 9 /)='
| Thank you, | ( (_) )
| Management | /`-vvv-'\
+-------------------+ / \
| | @@@ / /|,,,,,|\ \
| | @@@ /_// /^\ \\_\
@x@@x@ | | |/ WW( ( ) )WW
\||||/ | | \| __\,,\ /,,/__
\||/ | | | jgs (______Y______)
/\/\/\/\/\/\/\/\//\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\


jue

Mark Tarver

unread,
Feb 14, 2011, 8:32:36 AM2/14/11
to
In Qi

(35-) (group [[0 a b] [1 c d] [2 e f] [3 g h] [1 i j]
[2 k l] [4 m n] [2 o p] [4 q r] [5 s t]])
[[a b] [g h] [c d i j] [e f k l o p] [m n q r] [s t]]

Program is:

(define group
L -> (map (grouph L) (rd (map head L))))

(define grouph
[] _ -> []
[[X | Y] | Z] X -> (append Y (grouph Z X))
[_ | Y] X -> (grouph Y X))

(define rd
[] -> []
[X | Y] -> (if (element? X Y) (rd Y) [X | (rd Y)]))

Couldn't post this under OP because this option was not given under
Google news.

Mark

WJ

unread,
Feb 17, 2011, 2:19:43 AM2/17/11
to
Xah Lee wrote:

> here's a interesting toy list processing problem.
>
> I have a list of lists, where each sublist is labelled by
> a number. I need to collect together the contents of all sublists
> sharing
> the same label. So if I have the list
>
> ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
> r) (5 s t))
>
> where the first element of each sublist is the label, I need to
> produce:
>
> output:
> ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
>

Solving without hash-tables or "group-by".

Using Guile:

First, sort the groups by the numbers.

(stable-sort groups (lambda(a b)(< (car a) (car b))))

((0 a b) (1 c d) (1 i j) (2 e f) (2 k l) (2 o p) (3 g h)
(4 m n) (4 q r) (5 s t))

Next, flatten the list.

(append-map identity step1)

(0 a b 1 c d 1 i j 2 e f 2 k l 2 o p 3 g h 4 m n 4 q r 5 s t)

Remove duplicate numbers.

(delete-duplicates step2)

(0 a b 1 c d i j 2 e f k l o p 3 g h 4 m n q r 5 s t)

We now need a very useful function called "scan".

;; Yields sublists of contiguous items that satisfy func.
(define (scan func lst)
(let ((tail (find-tail func lst)))
(if tail
(cons (take-while func tail)
(scan func (drop-while func tail)))
'())))

(scan symbol? step3)

TheFlyingDutchman

unread,
Feb 17, 2011, 6:33:06 AM2/17/11
to
>
> Using Guile:

From Ruby to Clojure to Scheme. I think we can see where this is
heading, and I am sure I am not the only one wondering if you will be
able to find a MACLISP compiler that runs on the x86 architecture.

Marco Antoniotti

unread,
Feb 17, 2011, 7:02:05 AM2/17/11
to

x86? Z8000 more probably :)

Cheers
--
MA

Tim Bradshaw

unread,
Feb 17, 2011, 7:04:08 AM2/17/11
to

I think this is reasonably easy: there's a PDP-10 emulator which works
and I think will support ITS and hence MACLISP.

Jürgen Exner

unread,
Feb 17, 2011, 8:24:24 PM2/17/11
to
"WJ" <w_a_...@yahoo.com> wrote:

WJ

unread,
Feb 18, 2011, 5:47:58 AM2/18/11
to
WJ wrote:

> Xah Lee wrote:
>
> > here's a interesting toy list processing problem.
> >
> > I have a list of lists, where each sublist is labelled by
> > a number. I need to collect together the contents of all sublists
> > sharing
> > the same label. So if I have the list
> >
> > ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4
> > q r) (5 s t))
> >
> > where the first element of each sublist is the label, I need to
> > produce:
> >
> > output:
> > ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
> >
>
> Solving without hash-tables or "group-by".
>
> Using Guile:
>

(define groups '((0 a b)(1 c d)(2 e f)(3 g h)(1 i j)(2 k l)
(4 m n) (2 o p)(4 q r) (5 s t)))

(define keys (delete-duplicates (map car groups)))

(define (meld groups) (apply append (map cdr groups)))

(map (lambda (k)
(meld (filter (lambda (x)(eq? k (car x))) groups)))
keys)

0 new messages