Symboling dotted list
by Slobodan Blazeski » Mon, 04 May 2009 20:22:39 GMT
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
Re: Symboling dotted list
by pjb » Mon, 04 May 2009 20:55:33 GMT
Slobodan Blazeski < XXXX@XXXXX.COM > writes:
Have a look at:
http://www.**--****.com/
http://www.**--****.com/
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__
Re: Symboling dotted list
by Slobodan Blazeski » Tue, 05 May 2009 17:41:23 GMT
On May 3, 1:55m, XXXX@XXXXX.COM (Pascal J. Bourguignon)
> (defun tree-children (tree) tr>e)
> (defun leafp (tree) (at>m >ree))
>
> (defun add-symbol (keyword-designating-the-symbol-name-why-is-it-so-I-won>er tree)
> (add-symbol-1 (intern (string keyword-designating-the-symbol-name-why-is-it-so->-wonder))
> > gt;tree))
>
> (defun add-symb>l-1 (symbol tree)
>>(if (leafp tree)
> tree > no change on a leaf.
> (make-tree (cons symbol (map-dotted-list (lambda (subtree) (add->ymbol-1 symbol subtree))
> gt;> tree-children tree))))))
>
> To process a dotte> list, the difference is that instead of getting
> NIL when you reach >he end of the list, you get a atom. o you only
> have >o >hance one test to the normal list processin>:
>
> (>efun map-dotted-list (fun dotted-list)
> (cond
>> ((nu>l dotted-list) ; it wasn't a dotted list
> ())
> ((ato> dotted-list) ; this is the last>item of the dotted list.
> > funcall fun dotted-list))
> (t ; this is a cons cell:
> cons (funcal> f>n (ca> dotted-list)) (map-dotted-list fun (cdr dotted-list))))))
>
> --
> __Pascal Bourguignon__
Many thanks. Great code and great read.
bobi
Similar Threads:
1.What's the idea with dotted list + Replace and dotted lists
I wonder why doesn't a truckload of functions work with dotted list?
You like length, endp(*) etc
bobi
(*)(defun inc (lst)
(if (endp lst) 0 (1+ (inc (cdr lst)))))
INC
(inc '(1 2 3 4))
4
(inc '(1 2 3 . 4)
error
2.Sort a list of dotted pairs
Dear group,
I have a list of dotted pairs that may look something like this:
((4 . something) (2 . maybe ) (3 . this ) (7 . last ) (5 . some))
I want it to be sorted after the car of each dotted pair starting with
the lowest number...
((2 . maybe) (3 . this) (4 . something) (5 . some) (7 . last))
Only thing I have done so far is the sort function which does not seem
to help in that case... How could this be done? Thanks.
3.Detection of dotted list?
Hi All,
I have to detect whether or not an object is a dotted list. My solution is
(defvar *dotted-list* nil)
(defun dotted-list-p (l)
(let ((*dotted-list* *dotted-list*))
(push (car l) *dotted-list*)
(let ((c (cdr l)))
(when c
(if (consp c)
(dotted-list-p c)
(values t (nreverse *dotted-list*) c))))))
which returns the values of the predicate, the list before the dot and the final cdr.
CL-USER>(dotted-list-p '(1 2 3 4 5 6 7 . 8))
T
(1 2 3 4 5 6 7)
8
I think this function is end recursive and most implementation optimize it to a tail call.
Do you have any comments? Is there a more idomatic way to get these infos? Did I miss anything?
Andreas
4.keyed list with dot operation
Hi all,
I working on tcl and I would like to know how to use keyed lists with
dot operation?
please send ur suggestions ASAP.
Thanks in advance ..
____________
regards,
Anil A Kumar
5.volatile struct in dot h vs dot c
Hello,
Can you tell me is it my imagination, or should I not stick a "volatile"
definition into a dot h file?
My problem is if I put my declaration in main.c before main () my code
works. But, if I move it to a dot h and include the dot h into main.c, I
get an error when I run the program.
I am using the volatile qualifier because I am using mmap.
Any suggestions,
Christopher Lusardi
6. Lists within Lists or Nested Linked Lists...
7. consing a list-of-numbers to a list-of-lists-of-numbers
8. News server for dot net