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))
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)))))
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))
Kenny Tilton wrote:
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)))))
Gauche Scheme
(use gauche.collection) ;; group-collection
(define (meld groups)
(map (lambda(xs) (cons (caar xs) (map cadr xs))) groups))
(define (join-similar pairs)
(meld (group-collection pairs :key car :test equal?)))
(join-similar '((foo 4)(bar 7)(foo 5)(bar 8)(fun 9)))
===>
((foo 4 5) (bar 7 8) (fun 9))
B. Pym 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))
Kenny Tilton wrote:
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)))))
Gauche Scheme
(use gauche.collection) ;; group-collection
(define (meld groups)
(map (lambda(xs) (cons (caar xs) (map cadr xs))) groups))
(define (join-similar pairs)
(meld (group-collection pairs :key car :test equal?)))
(join-similar '((foo 4)(bar 7)(foo 5)(bar 8)(fun 9)))
===>
((foo 4 5) (bar 7 8) (fun 9))
Without "cheating" by using group-collection.
(define (join-similar pairs)
(let1 keys (delete-duplicates (map car pairs))
(map
(lambda(key)
(cons key
(map last (filter (lambda(xs) (equal? key (car xs))) pairs))))
keys)))
Kenny Tilton wrote:
(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)))))
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))
Kenny Tilton wrote:
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)))))
B. Pym 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))
Kenny Tilton wrote:
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)))))
Gauche Scheme
Using a collector that collects into an association list.
(define (join-similar pairs)
(let1 a (malistbag)
(dolist (xs pairs) (a (car xs) (cadr xs) cons ()))
(a)))
(join-similar '((foo 4)(bar 7)(foo 5)(bar 8)(fun 9)))
===>
((fun 9) (foo 5 4) (bar 8 7))
Given:
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 65 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 07:17:02 |
| Calls: | 862 |
| Files: | 1,311 |
| Messages: | 264,829 |