[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: Substitutions in a tree

William James

9/3/2015 3:52:00 PM

Wade Humeniuk wrote:

> > So some examples:
> > (subtreefun tree oldsymbol newsymbol1 newsymbol2) ===> Subedtree
> > (subtreefun (1 2 (3 4 5) 6 4) '4 'a 'b) ===> (1 2 (3 a b 5) 6 a b)
> >
> >
> > This function does this substitution but for only one new entry, how
> > would an n new entry function look?
> > (defun sub (tree old new)
> > (cond ((null tree) nil)
> > ((equal tree old) new )
> > ((atom tree) tree)
> > (t (cons (sub (car tree) old new)
> > (sub (cdr tree) old new)))))
> >
>
> (defun subtreefun (tree old &rest new)
> (mapcan (lambda (element)
> (typecase element
> (atom (if (eql element old)
> new
> (list element)))
> (list (list (apply #'subtreefun element old new)))))
> tree))
>
>


Consider this line:

(list (list (apply #'subtreefun element old new)))

It seems that the function "list" is being used twice, but
that is not what is happening.

CL (COBOL-Like) is a truly atrocious language---ugly and
clunky and warty beyond belief.

That's why the sports love it so.


> CL-USER 14 > (subtreefun '(1 2 (3 4 5) 6 4) 4 'a 'b)
> (1 2 (3 A B 5) 6 A B)
>
> CL-USER 15 > (subtreefun '(1 2 (3 4 5) 6 4) 4 'a 'b 'c 'x 's)
> (1 2 (3 A B C X S 5) 6 A B C X S)

Gauche Scheme:

(define (sub-in-tree tree old . new)
(append-map
(^x
(cond ((equal? x old) new)
((pair? x) (list (apply sub-in-tree x old new)))
(else (list x))))
tree))

gosh> (sub-in-tree '(1 2 (3 4 5) 6 4) 4 'a 'b)
(1 2 (3 a b 5) 6 a b)
gosh> (sub-in-tree '(1 2 (3 4 (4) 5) 6 4) 4 'a 'b)
(1 2 (3 a b (a b) 5) 6 a b)
gosh> (sub-in-tree '(1 2 (3 4 5) 6 4) '(3 4 5) 'a 'b)
(1 2 a b 6 4)

--
H]e was prosecuted ... for "denying" the gas chambers and the six million
figure. In July 1998 a Swiss court sentenced him to 15 months imprisonment,
and to pay a large fine, because of his writings. Rather than serve the
sentence, in August 2000 Graf went into exile. In 2001 he married a Russian
historian in Moscow. www.revisionists.com/revisionists/graf.html
1 Answer

William James

10/31/2015 5:55:00 PM

0

WJ wrote:

> Wade Humeniuk wrote:
>
> > > So some examples:
> > > (subtreefun tree oldsymbol newsymbol1 newsymbol2) ===> Subedtree
> > > (subtreefun (1 2 (3 4 5) 6 4) '4 'a 'b) ===> (1 2 (3 a b 5) 6 a b)
> > >
> > >
> > > This function does this substitution but for only one new entry, how
> > > would an n new entry function look?
> > > (defun sub (tree old new)
> > > (cond ((null tree) nil)
> > > ((equal tree old) new )
> > > ((atom tree) tree)
> > > (t (cons (sub (car tree) old new)
> > > (sub (cdr tree) old new)))))
> > >
> >
> > (defun subtreefun (tree old &rest new)
> > (mapcan (lambda (element)
> > (typecase element
> > (atom (if (eql element old)
> > new
> > (list element)))
> > (list (list (apply #'subtreefun element old new)))))
> > tree))
> >
> >
>
>
> Consider this line:
>
> (list (list (apply #'subtreefun element old new)))
>
> It seems that the function "list" is being used twice, but
> that is not what is happening.
>
> CL (COBOL-Like) is a truly atrocious language---ugly and
> clunky and warty beyond belief.
>
> That's why the sports love it so.
>
>
> > CL-USER 14 > (subtreefun '(1 2 (3 4 5) 6 4) 4 'a 'b)
> > (1 2 (3 A B 5) 6 A B)
> >
> > CL-USER 15 > (subtreefun '(1 2 (3 4 5) 6 4) 4 'a 'b 'c 'x 's)
> > (1 2 (3 A B C X S 5) 6 A B C X S)
>
> Gauche Scheme:
>
> (define (sub-in-tree tree old . new)
> (append-map
> (^x
> (cond ((equal? x old) new)
> ((pair? x) (list (apply sub-in-tree x old new)))
> (else (list x))))
> tree))
>
> gosh> (sub-in-tree '(1 2 (3 4 5) 6 4) 4 'a 'b)
> (1 2 (3 a b 5) 6 a b)
> gosh> (sub-in-tree '(1 2 (3 4 (4) 5) 6 4) 4 'a 'b)
> (1 2 (3 a b (a b) 5) 6 a b)
> gosh> (sub-in-tree '(1 2 (3 4 5) 6 4) '(3 4 5) 'a 'b)
> (1 2 a b 6 4)

MatzLisp (Ruby):

def sub_in_tree(tree, old, *new)
tree.flat_map{|x|
if x == old
new
elsif x.is_a? Array
[sub_in_tree( x, old, *new )]
else
[x]
end}
end

sub_in_tree [1,2,[3,4,5],6,4], 4, :a,:b
==>[1, 2, [3, :a, :b, 5], 6, :a, :b]

sub_in_tree [1,2,[3,4,5],6,4], [3,4,5], :a,:b
==>[1, 2, :a, :b, 6, 4]

--
For two years ... he was held in solitary confinement in the Toronto West
Detention Centre, on the pretext that he is a threat to national security....
[A] court in Mannheim sentenced him to five years imprisonment for the crime of
"popular incitement" under Germany's notorious "Holocaust denial" statute.
http://www.revisionists.com/revisionists/z...