From: ·······@yahoo.com
Subject: need help with data structure problem
Date: 
Message-ID: <3bda7e88.7970980@news.dallas.sbcglobal.net>
hi, I need to write a function  (join_similar expr) where expr is
adata structure with the following format ( (x1 y1) (x2 y2) (x3 y3)...
(xn yn)),

join_similar  will return an expression like ( (x1 y1 y2) (x3 y3) ...)
when x1=x2 

for instance:
*(join_similar �((3 4) (7 5) (3 6) (7 8) (3 9) (0 1))

would return:
((3 4 6 9) (7 5 8) (0 1))

I am very new to lisp and am having a great deal of problems trying to
implement this.  the steps that i'm thinking of implementing is:

take first of expr
take first of that first
save that in a variable
check that variable against the first of every other element
if the firsts of any other element matches that variable, cons it into
a new list
do the same for all others members of expr.  

as you can see, i need alot of help.  any and all help is appreciated
greatly :)

thanks in advance

From: Kenny Tilton
Subject: Re: need help with data structure problem
Date: 
Message-ID: <3BDAAE90.7292FCD2@nyc.rr.com>
·······@yahoo.com wrote:
> 
> hi, I need to write a function  (join_similar expr) where expr is
> adata structure with the following format ( (x1 y1) (x2 y2) (x3 y3)...
> (xn yn)),
> 
> join_similar  will return an expression like ( (x1 y1 y2) (x3 y3) ...)
> when x1=x2
> 
> for instance:
> *(join_similar �((3 4) (7 5) (3 6) (7 8) (3 9) (0 1))
> 
> would return:
> ((3 4 6 9) (7 5 8) (0 1))
> 
> I am very new to lisp and am having a great deal of problems trying to
> implement this.  the steps that i'm thinking of implementing is:

seems like a reasonable start. code it up and see what happens. some
thoughts:

> 
> take first of expr
> take first of that first
> save that in a variable
> check that variable against the first of every other element

might be a bug here in checking "every other". you might fail to collect
the value from the first occurrence and get: ((3 6 9)(7 8)(0)) 

> if the firsts of any other element matches that variable, cons it into
> a new list

"it"? don't you want the second element of "it"?

> do the same for all others members of expr.

not really. once you have collected all the threes your top level loop
can skip the threes when it encounters them again working thru the expr.

> 
> as you can see, i need alot of help.  any and all help is appreciated
> greatly :)
> 

try breaking the problem down:

first see if you can just produce a list of the keys wihout duplicates:
(3 7 0)

then with that under control worry about collecting the matches.

kenny
clinisys
From: Lieven Marchand
Subject: Re: need help with data structure problem
Date: 
Message-ID: <m3adydxnqi.fsf@localhost.localdomain>
·······@yahoo.com writes:

> hi, I need to write a function  (join_similar expr) where expr is
> adata structure with the following format ( (x1 y1) (x2 y2) (x3 y3)...
> (xn yn)),

Names with underscores aren't used very often in Lisp. The usual
separator character is a hyphen. So let's write join-similar.

> join_similar  will return an expression like ( (x1 y1 y2) (x3 y3) ...)
> when x1=x2 
> 
> for instance:
> *(join_similar �((3 4) (7 5) (3 6) (7 8) (3 9) (0 1))

Try not to spread the Microsoftian smart quotes. In my news reader
this came out as \221((...

> would return:
> ((3 4 6 9) (7 5 8) (0 1))

Is the ordering significant or not?

> I am very new to lisp and am having a great deal of problems trying to
> implement this.  the steps that i'm thinking of implementing is:
> 
> take first of expr
> take first of that first
> save that in a variable
> check that variable against the first of every other element
> if the firsts of any other element matches that variable, cons it into
> a new list
> do the same for all others members of expr.  

How many times is this going to traverse your list? Is there a way to
do this better?

> as you can see, i need alot of help.  any and all help is appreciated
> greatly :)

Your problem sounds strongly like a homework problem. It's considered
polite to mention this when seeking help, since most people here don't
find it useful for the poster to do their homework for them.

However, here's an alternate solution with LOOP.

CL-USER 9 > (defun join-similar (list)
              (loop with ht = (make-hash-table)
                    for (first second) in list
                    do
                    (pushnew second (gethash first ht nil))
                    finally (return (loop for first being each hash-key in ht using (hash-value rest) collect (cons first rest)))))
JOIN-SIMILAR

Isn't LOOP beautiful? <g,d&r>

-- 
Lieven Marchand <···@wyrd.be>
She says, "Honey, you're a Bastard of great proportion."
He says, "Darling, I plead guilty to that sin."
Cowboy Junkies -- A few simple words
From: Kenny Tilton
Subject: Re: need help with data structure problem
Date: 
Message-ID: <3BDAF29C.E59BF003@nyc.rr.com>
Lieven Marchand wrote:
> CL-USER 9 > (defun join-similar (list)
>               (loop with ht = (make-hash-table)
>                     for (first second) in list
>                     do
>                     (pushnew second (gethash first ht nil))
>                     finally (return (loop for first being each hash-key in ht using (hash-value rest) collect (cons first rest)))))
> JOIN-SIMILAR
> 
> Isn't LOOP beautiful? <g,d&r>

<g> No...

(defun join-similar (pairs &aux result)
  (dolist (pair pairs (nreverse result))
    (nconc (or (assoc (first pair) result)
               (first (push (list (first pair)) result)))
           (list (second pair)))))

.../Lisp/ is beautiful. <g>

kenny
clinisys
From: Marcin Tustin
Subject: Re: need help with data structure problem
Date: 
Message-ID: <yztbitd0r9m4.fsf@benedict.i-did-not-set--mail-host-address--so-shoot-me>
·······@yahoo.com writes:

Include standard comments about homework.

> hi, I need to write a function  (join_similar expr) where expr is
> adata structure with the following format ( (x1 y1) (x2 y2) (x3 y3)...
> (xn yn)),
> 
> join_similar  will return an expression like ( (x1 y1 y2) (x3 y3) ...)
> when x1=x2 
> 
> for instance:
> *(join_similar �((3 4) (7 5) (3 6) (7 8) (3 9) (0 1))
> 
> would return:
> ((3 4 6 9) (7 5 8) (0 1))
> 
> I am very new to lisp and am having a great deal of problems trying to
> implement this.  the steps that i'm thinking of implementing is:

        Try sorting the list using a mergesort or good quicksort
(commonlisp sort is typically quicksort), then you have all of
the same keys together. I also liked the loop solution posted
elsewhere (Being new to CL, didn't really understand it - must
learn loop)

> take first of expr
> take first of that first
> save that in a variable
> check that variable against the first of every other element
> if the firsts of any other element matches that variable, cons it into
> a new list
> do the same for all others members of expr.  
> 
> as you can see, i need alot of help.  any and all help is appreciated
> greatly :)
> 
> thanks in advance