Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
How to destructure list to places?
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
  9 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
 
WJ  
View profile  
 More options Mar 19 2012, 8:45 pm
Newsgroups: comp.lang.lisp
From: "WJ" <w_a_x_...@yahoo.com>
Date: 20 Mar 2012 00:45:44 GMT
Local: Mon, Mar 19 2012 8:45 pm
Subject: Re: How to destructure list to places?

Matthew Danish wrote:
> (destructuring-bind (a b (c d)) (list 1 2 (list 3 4))
>   (list b c d a))

Clojure:

(let [[a b [c d]] (list 1 2 (list 3 4))]
  (list b c d a))

  ==> (2 3 4 1)


 
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.
WJ  
View profile  
 More options May 10 2012, 11:53 pm
Newsgroups: comp.lang.lisp
From: "WJ" <w_a_x_...@yahoo.com>
Date: 11 May 2012 03:53:12 GMT
Local: Thurs, May 10 2012 11:53 pm
Subject: Re: How to destructure list to places?

WJ wrote:
> Matthew Danish wrote:

> > (destructuring-bind (a b (c d)) (list 1 2 (list 3 4))
> >   (list b c d a))

> Clojure:

> (let [[a b [c d]] (list 1 2 (list 3 4))]
>   (list b c d a))

>   ==> (2 3 4 1)

Racket:

(match-let ([`(,a ,b (,c ,d)) '(1 2 (3 4))]) (list b c d a))
 => '(2 3 4 1)
(match-let ([(list a b (list c d)) '(1 2 (3 4))]) (list b c d a))
 => '(2 3 4 1)

(match-let ([(list (? odd? odds) ... more ...) '(3 5 7 8 9)])
  (list odds more))
 => '((3 5 7) (8 9))


 
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.
WJ  
View profile  
 More options May 10 2012, 11:59 pm
Newsgroups: comp.lang.lisp
From: "WJ" <w_a_x_...@yahoo.com>
Date: 11 May 2012 03:59:56 GMT
Local: Thurs, May 10 2012 11:59 pm
Subject: Re: How to destructure list to places?

'((1 2 3) (4 5))

 
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.
Norbert_Paul  
View profile  
 More options May 11 2012, 3:33 am
Newsgroups: comp.lang.lisp
From: Norbert_Paul <norbertpauls_spam...@yahoo.com>
Date: Fri, 11 May 2012 09:33:22 +0200
Local: Fri, May 11 2012 3:33 am
Subject: Re: How to destructure list to places?
I once did it for fun to have sort of a "poor man's struct".

Here
   (destructuring-bind tree . body)
is equivalent to
   (with-cons-places (tree) . body)
.
In other words: (tree) is a forest which consists of one tree.

----%<-------------------------------------------------------%<----

(defmacro with-cons-places (forest &body body)

   "Locally defines accessors via flet to the leafs of each car/cdr tree defined in the
specified forest - a list of lists. Each list in the specified forest defines one tree.
So it is possible to define a hierarchy of accessors to non-leaf-nodes.

Each non-nil symbol acc which occurs in one tree lst denotes an accessor acc
such that

  (eq (acc lst) 'acc) is true.

Keyword symbols will be ignored. So if one specifies ((a b) . :ignore) as a tree
only the accessors a (with #'a = #'caar and #(setf a) = #'(setf caar))
and b (with #'b = #'cadar and #'(setf b) = #'(setf cadar)) will be defined.

Examples:

   (with-cons-places ((search-key . attached-value))
     (let ((data '((:a-key       . :a-value)
                   (:another-key . :another-value)
                   (:some-key     . :old-value))))
       (setf (attached-value (find :some-key data :key #'search-key))
             :new-value )
       (attached-value (find :some-key data :key #'search-key))))
   ==>
   :new-value

   (with-cons-places (( row-col  . attached-value)
                      ((row col) . :dont-ignore)) ; will be ignored anyway
     (let ((matrix '( ((1 1) . :a11) ((1 2) . :a12) ((1 3) . :a13)
                                     ((2 2) . :a22)
                      ((3 1) . :a31)                ((3 3) . :a33))))
        (flet ((row-n  (mat n) (remove n mat
                                      :key #'row
                                      :test (complement #'eql)))
               (col-m  (mat m) (remove m mat
                                      :key #'col
                                      :test (complement #'eql))))
          (col-m matrix 2))))
    ==>
    (((1 2) . :A12) ((2 2) . :A22))

   (with-cons-places ((() () () () () () () () () () eleventh twelvth thirteenth))
      (eleventh '(1 2 3 4 5 6 7 8 9 10 12)))
   ==> 12

   (let ((lst (list 1 2 3 4 5 6 7 8 9 10 12)))
     (with-cons-places ((() () () () () () () () () () eleventh twelvth thirteenth))
        (setf (eleventh lst) 11)
        lst ))
   ==> (1 2 3 4 5 6 7 8 9 10 11)
"

   (labels ((cons-places (tree &optional (result ()))

             (cond ((null tree)     ())
                   ((keywordp tree) ())   ; ignore keywords
                   ((symbolp tree)  (list (cons tree ; well, actually, its leaf.
                                                (reverse result))))
                   ((atom tree)     ()) ; this is an error.
                   (T
                    (append
                     (cons-places (car tree) (cons 'car result))
                     (cons-places (cdr tree) (cons 'cdr result))))))

           ;; concatenates sequences (cxr cyr czr) into czyxr or fourth, ..., tenth
           ;; note the inversion in order from (.x. .y. .z.) to .zyx.
           (pack-cons-place (lst &aux (pos 0))
             (cond ((null lst) nil)
                   ((null (cdr lst)) lst)
                   ((null (cddr lst))
                    (cond ((equal lst '(car car)) '(caar))
                          ((equal lst '(cdr car)) '(cadr))
                          ((equal lst '(car cdr)) '(cdar))
                          ((equal lst '(cdr cdr)) '(cddr))
                          (T lst)))     ; be safe
                   ((null (cdddr lst))
                    (cond ((equal lst '(car car car)) '(caaar))
                          ((equal lst '(car cdr car)) '(cadar))
                          ((equal lst '(car car cdr)) '(cdaar))
                          ((equal lst '(car cdr cdr)) '(cddar))
                          ((equal lst '(cdr car car)) '(caadr))
                          ((equal lst '(cdr cdr car)) '(caddr))
                          ((equal lst '(cdr car cdr)) '(cdadr))
                          ((equal lst '(cdr cdr cdr)) '(cdddr))
                          ( T lst)))    ; be safe
                   ((null (cddddr lst))
                    (cond ((equal lst '(car car car car)) '(caaaar))
                          ((equal lst '(car car cdr car)) '(cadaar))
                          ((equal lst '(car car car cdr)) '(cdaaar))
                          ((equal lst '(car car cdr cdr)) '(cddaar))
                          ((equal lst '(car cdr car car)) '(caadar))
                          ((equal lst '(car cdr cdr car)) '(caddar))
                          ((equal lst '(car cdr car cdr)) '(cdadar))
                          ((equal lst '(car cdr cdr cdr)) '(cdddar))
                          ((equal lst '(cdr car car car)) '(caaadr))
                          ((equal lst '(cdr car cdr car)) '(cadadr))
                          ((equal lst '(cdr car car cdr)) '(cdaadr))
                          ((equal lst '(cdr car cdr cdr)) '(cddadr))
                          ((equal lst '(cdr cdr car car)) '(caaddr))
                          ((equal lst '(cdr cdr cdr car)) '(cadddr))
                          ((equal lst '(cdr cdr car cdr)) '(cdaddr))
                          ((equal lst '(cdr cdr cdr cdr)) '(cddddr))
                          ( T lst)))    ; be safe

                   ;; first to tenth are applicable.
                   ;;  Note: first to fourth are already handled by the previous cases:
                   ;;    car, cadr, caddr, or cadddr.
                   ((< (setf pos (position 'car lst)) ; relies on the fact, that only car and cdr exists in lst
                       9)
                    (cons (nth pos '(first second third fourth fifth sixth seventh eighth ninth tenth))
                          (pack-cons-place (nthcdr pos (cdr lst)))))

                   ;; split up the sequence into subsequences of length at most 4.
                   (T (append (pack-cons-place (list (car lst) (cadr lst) (caddr lst) (cadddr lst)))
                              (pack-cons-place (cddddr lst)))))))

     (let* ((accessors (mapcan #'cons-places forest))
           (names     (mapcar #'car accessors))
           (functions (mapcan #'(lambda (name-acc)
                                  (let ((name (car name-acc))
                                        (accessor
                                         (pack-cons-place
                                          (cdr name-acc)))
                                        (result 'X))
                                    (dolist (acc accessor)
                                      (setf result `(,acc ,result)))
                                    `((,name          (X) ,result) ; the reader
                                      ((setf ,name) (Y X) (setf ,result Y))))) ; the writer
                              accessors)))
       `(flet ,functions
         (declare (ignorable ,@(mapcan #'(lambda (name)
                                           `((function ,name) (function (setf ,name))))
                                       names)))
         . ,body))))

----%<-------------------------------------------------------%<----

Norbert


 
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.
Tim Bradshaw  
View profile  
 More options May 11 2012, 3:47 am
Newsgroups: comp.lang.lisp
From: Tim Bradshaw <t...@tfeb.org>
Date: Fri, 11 May 2012 08:47:09 +0100
Local: Fri, May 11 2012 3:47 am
Subject: Re: How to destructure list to places?
On 2012-05-11 03:53:12 +0000, WJ said:

> ([`(,a ,b (,c ,d)) '(1 2 (3 4))])

Oh man, more of this shit.  Two kinds of brackets, two kinds of quotes
neither of which matches, and commas in front of things?  Yuck.

 
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.
Espen Vestre  
View profile  
 More options May 11 2012, 3:57 am
Newsgroups: comp.lang.lisp
From: Espen Vestre <es...@vestre.net>
Date: Fri, 11 May 2012 09:57:28 +0200
Local: Fri, May 11 2012 3:57 am
Subject: Re: How to destructure list to places?

Tim Bradshaw <t...@tfeb.org> writes:
> Oh man, more of this shit.  Two kinds of brackets, two kinds of quotes
> neither of which matches, and commas in front of things?  Yuck.

Can't you just put him in your kill file? He has the honor of being the
only person in my kill file, so I just see the noise when you and others
keep commenting it.
--
  (espen)

 
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.
Tim Bradshaw  
View profile  
 More options May 11 2012, 6:09 am
Newsgroups: comp.lang.lisp
From: Tim Bradshaw <t...@tfeb.org>
Date: Fri, 11 May 2012 11:09:20 +0100
Local: Fri, May 11 2012 6:09 am
Subject: Re: How to destructure list to places?
On 2012-05-11 07:57:28 +0000, Espen Vestre said:

> Can't you just put him in your kill file? He has the honor of being the
> only person in my kill file, so I just see the noise when you and others
> keep commenting it.

No! Baiting him is the sole purpose of my existence.  It took me a long
time to realise this: I assumed my purpose was to lounge on the lawn in
the smaller rose garden, occasionally calling for another whisky and
soda[*], but no, I now realise it is to bait WJ. I have my newsreader
set up so his articles flash red, sound bells and cause immediate
termination of all other applications on my computing appliance, and
the butler has instructions to wake me at any time, day or night, if he
posts.  He has so far proved unwilling to do so, but I will have words.

I'm afraid your only solution is to put me in your kill file.  I'd
suggest everyone do that, in fact.

--t

[*] In the summer, obviously: in the winter I am to be found in the
library, or sometimes on my yacht, which I have kept in a sunnier part
of the world.


 
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.
Espen Vestre  
View profile  
 More options May 11 2012, 6:32 am
Newsgroups: comp.lang.lisp
From: Espen Vestre <es...@vestre.net>
Date: Fri, 11 May 2012 12:32:17 +0200
Local: Fri, May 11 2012 6:32 am
Subject: Re: How to destructure list to places?

Tim Bradshaw <t...@tfeb.org> writes:
> No! Baiting him is the sole purpose of my existence.  

LOL :-)

> I'm afraid your only solution is to put me in your kill file.  I'd
> suggest everyone do that, in fact.

I'm sorry Dave^WTim, I can't do that after reading this!
--
  (espen)

 
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.
Tim Bradshaw  
View profile  
 More options May 11 2012, 6:44 am
Newsgroups: comp.lang.lisp
From: Tim Bradshaw <t...@tfeb.org>
Date: Fri, 11 May 2012 11:44:55 +0100
Local: Fri, May 11 2012 6:44 am
Subject: Re: How to destructure list to places?
On 2012-05-11 10:32:17 +0000, Espen Vestre said:

> I'm sorry Dave^WTim, I can't do that after reading this!

It's OK, I'll send a man round to do it.

 
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 »