From: Slobodan Blazeski
Subject: Symboling dotted list
Date: 
Message-ID: <ac21a90c-2305-4ef0-966d-e9e9fb9625a0@t10g2000vbg.googlegroups.com>
This is homework :) please don't tell my teacher.
I have a problem adding symbol as a first element over a dotted list,
actually tree.

(add-symbol :foo 1)
1

(add-symbol :foo '(1 . 2))
(foo  1 . 2)

(add-symbol :foo '(1 (2 3 . 4) 5))
(foo 1 (foo 2 3 . 4) 5)

The solution for regular list is easy with dolist or loop/colect but
how to loop the dotted list.

thanks
bobi

From: Pascal J. Bourguignon
Subject: Re: Symboling dotted list
Date: 
Message-ID: <877i0y764q.fsf@galatea.local>
Slobodan Blazeski <·················@gmail.com> writes:

> This is homework :) please don't tell my teacher.
> I have a problem adding symbol as a first element over a dotted list,
> actually tree.
>
> (add-symbol :foo 1)
> 1
>
> (add-symbol :foo '(1 . 2))
> (foo  1 . 2)
>
> (add-symbol :foo '(1 (2 3 . 4) 5))
> (foo 1 (foo 2 3 . 4) 5)
>
> The solution for regular list is easy with dolist or loop/colect but
> how to loop the dotted list.

Have a look at:
http://groups.google.com/group/comp.lang.lisp/msg/b8d9376744b4ebb1
http://groups.google.com/group/comp.lang.lisp/msg/0c66e597e08be90d

So you have trees.  It looks like the nodes of these trees have no
label, only a list of children, and for some strange reason, this can
be a dotted-list.

(defun make-tree     (children) children)
(defun tree-children (tree)     tree)
(defun leafp         (tree)     (atom tree))

(defun add-symbol (keyword-designating-the-symbol-name-why-is-it-so-I-wonder tree)
  (add-symbol-1 (intern (string keyword-designating-the-symbol-name-why-is-it-so-I-wonder))
                tree))

(defun add-symbol-1 (symbol tree)
  (if (leafp tree)
      tree ; no change on a leaf.
      (make-tree (cons symbol (map-dotted-list (lambda (subtree) (add-symbol-1 symbol subtree))
                                               (tree-children tree))))))

To process a dotted list, the difference is that instead of getting
NIL when you reach the end of the list, you get a atom.  So you only
have to chance one test to the normal list processing:

(defun map-dotted-list (fun dotted-list)
  (cond
    ((null dotted-list) ; it wasn't a dotted list
       '())
    ((atom dotted-list) ; this is the last item of the dotted list.
       (funcall fun dotted-list))
    (t ; this is a cons cell:
       (cons (funcall fun (car dotted-list)) (map-dotted-list fun (cdr dotted-list))))))



-- 
__Pascal Bourguignon__
From: Slobodan Blazeski
Subject: Re: Symboling dotted list
Date: 
Message-ID: <bccdc104-b25c-4fff-9487-b72faf5e4cde@s31g2000vbp.googlegroups.com>
On May 3, 1:55 pm, ····@informatimago.com (Pascal J. Bourguignon)
wrote:
> Slobodan Blazeski <·················@gmail.com> writes:
> > This is homework :) please don't tell my teacher.
> > I have a problem adding symbol as a first element over a dotted list,
> > actually tree.
>
> > (add-symbol :foo 1)
> > 1
>
> > (add-symbol :foo '(1 . 2))
> > (foo  1 . 2)
>
> > (add-symbol :foo '(1 (2 3 . 4) 5))
> > (foo 1 (foo 2 3 . 4) 5)
>
> > The solution for regular list is easy with dolist or loop/colect but
> > how to loop the dotted list.
>
> Have a look at:http://groups.google.com/group/comp.lang.lisp/msg/b8d9376744b4ebb1http://groups.google.com/group/comp.lang.lisp/msg/0c66e597e08be90d
>
> So you have trees.  It looks like the nodes of these trees have no
> label, only a list of children, and for some strange reason, this can
> be a dotted-list.
>
> (defun make-tree     (children) children)
> (defun tree-children (tree)     tree)
> (defun leafp         (tree)     (atom tree))
>
> (defun add-symbol (keyword-designating-the-symbol-name-why-is-it-so-I-wonder tree)
>   (add-symbol-1 (intern (string keyword-designating-the-symbol-name-why-is-it-so-I-wonder))
>                 tree))
>
> (defun add-symbol-1 (symbol tree)
>   (if (leafp tree)
>       tree ; no change on a leaf.
>       (make-tree (cons symbol (map-dotted-list (lambda (subtree) (add-symbol-1 symbol subtree))
>                                                (tree-children tree))))))
>
> To process a dotted list, the difference is that instead of getting
> NIL when you reach the end of the list, you get a atom.  So you only
> have to chance one test to the normal list processing:
>
> (defun map-dotted-list (fun dotted-list)
>   (cond
>     ((null dotted-list) ; it wasn't a dotted list
>        '())
>     ((atom dotted-list) ; this is the last item of the dotted list.
>        (funcall fun dotted-list))
>     (t ; this is a cons cell:
>        (cons (funcall fun (car dotted-list)) (map-dotted-list fun (cdr dotted-list))))))
>
> --
> __Pascal Bourguignon__

Many thanks. Great code and great read.

bobi