[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

need help with tree substitution

hans

9/11/2015 11:28:00 AM

I have a large tree with many nested (:note "string") at any level, and am trying to replace any (:note "..") list with its string.

; this small example tree
'(a (:note "a")
(b (:note "abc"))
(d e f (:note "de"))))

;should be converted into this one
'(a "a"
(b "abc")
(d e f "de")))

i.e. recursively find every sublist beginning with :note and replace it with its string


I think also subst or subst-if could do such things, but am not able to compose a working function. help appreciated. thank you.
10 Answers

William James

9/11/2015 3:48:00 PM

0

schatzer.johann@gmail.com wrote:

> I have a large tree with many nested (:note
> "string") at any level, and am trying to
> replace any (:note "..") list with its
> string.
>
> ; this small example tree
> '(a (:note "a")
> (b (:note "abc"))
> (d e f (:note "de"))))
>
> ;should be converted into this one
> '(a "a"
> (b "abc")
> (d e f "de")))
>
> i.e. recursively find every sublist
> beginning with :note and replace it with its
> string

For what class is this homework?

Gauche Scheme:

(use util.match)

(define (convert tree)
(match tree
[(:note (? string? str)) str]
[(x ...) (map convert x)]
[x x]))

(a "a" (b "abc") (d e f "de"))

--
As Helle "Hamas" Klein, political editor of Sweden's largest newspaper
Aftonbladet, boasts: "If the debate is going to be about whether there are
problems with immigrants, we don't want it". Welcome to Sweden, the country
where the media doesn't even pretend to champion freedom of speech, but openly
brags about censorship.
jordman.blogspot.ca/2005/05/is-swedish-democracy-collapsing.html

Barry Margolin

9/11/2015 6:24:00 PM

0

In article <317ed58d-a5c0-4893-b04b-f09f9c38dfbb@googlegroups.com>,
schatzer.johann@gmail.com wrote:

> I have a large tree with many nested (:note "string") at any level, and am
> trying to replace any (:note "..") list with its string.
>
> ; this small example tree
> '(a (:note "a")
> (b (:note "abc"))
> (d e f (:note "de"))))
>
> ;should be converted into this one
> '(a "a"
> (b "abc")
> (d e f "de")))
>
> i.e. recursively find every sublist beginning with :note and replace it with
> its string

Like this:

(defun replace-note (tree)
(if (consp tree)
(if (eq (car tree) :note)
(cadr tree)
(cons (replace-note (car tree))
(replace-note (cdr tree))))
tree))

This returns a new tree with the substitutions, it's not done
destructively.

>
>
> I think also subst or subst-if could do such things, but am not able to
> compose a working function. help appreciated. thank you.

None of the SUBST family can do this, because the substitutions are
specified once in the arguments, they're not a function of the tree
contents.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

William James

9/11/2015 7:18:00 PM

0

WJ wrote:

> schatzer.johann@gmail.com wrote:
>
> > I have a large tree with many nested (:note
> > "string") at any level, and am trying to
> > replace any (:note "..") list with its
> > string.
> >
> > ; this small example tree
> > '(a (:note "a")
> > (b (:note "abc"))
> > (d e f (:note "de"))))
> >
> > ;should be converted into this one
> > '(a "a"
> > (b "abc")
> > (d e f "de")))
> >
> > i.e. recursively find every sublist
> > beginning with :note and replace it with its
> > string
>
> For what class is this homework?
>
> Gauche Scheme:
>
> (use util.match)
>
> (define (convert tree)
> (match tree
> [(:note (? string? str)) str]
> [(x ...) (map convert x)]
> [x x]))
>

(convert '(a (:note "a")
(b (:note "abc"))
(d e f (:note "de"))))


> (a "a" (b "abc") (d e f "de"))

William James

9/11/2015 7:21:00 PM

0

Barry Margolin wrote:

> In article <317ed58d-a5c0-4893-b04b-f09f9c38dfbb@googlegroups.com>,
> schatzer.johann@gmail.com wrote:
>
> > I have a large tree with many nested (:note "string") at any level, and am
> > trying to replace any (:note "..") list with its string.
> >
> > ; this small example tree
> > '(a (:note "a")
> > (b (:note "abc"))
> > (d e f (:note "de"))))
> >
> > ;should be converted into this one
> > '(a "a"
> > (b "abc")
> > (d e f "de")))
> >
> > i.e. recursively find every sublist beginning with :note and replace it with
> > its string
>
> Like this:
>
> (defun replace-note (tree)
> (if (consp tree)
> (if (eq (car tree) :note)
> (cadr tree)
> (cons (replace-note (car tree))
> (replace-note (cdr tree))))
> tree))

Gauche Scheme:

(define (replace-note tree)
(if (pair? tree)
(if (eq? :note (car tree))
(last tree)
(map replace-note tree))
tree))


(replace-note
'(a (:note "a") (b (:note "abc")) (d e f (:note "de"))))

===>
(a "a" (b "abc") (d e f "de"))

--
He has nothing but kind sentiments for those who would destroy his home and
family.... He is universally tolerant.... If he has any principles, he keeps
them well concealed.... He is, to the extent of his abilities, exactly like
the next citizen, who, he trusts, is trying to be exactly like him: a faceless,
characterless putty-man. --- Father Feeney; "Should Hate Be Outlawed?"

Kenneth Tilton

9/14/2015 2:23:00 PM

0

On Friday, September 11, 2015 at 7:28:06 AM UTC-4, schatze...@gmail.com wrote:
> I have a large tree with many nested (:note "string") at any level, and am trying to replace any (:note "..") list with its string.

Once upon a time I observed that any noob who did not dig past the syntactic sugar of the truly non-existent Lisp list type was doomed, and was roundly mocked by The Savages of comp.lang.lisp.

(Did we ever get those T-shirts made?)

So go back to your very first ever (Touretzky?) diagram of the phantasmagorical "list", assume the Lotus position, and await enlightenment.

Hint: a cons is a data structure with two slots, car and cdr. Either can be setf'ed but those of us who wax romantic over paper cuts from Lisp on punch cards use rplaca and rplacd.

-hk

>
> ; this small example tree
> '(a (:note "a")
> (b (:note "abc"))
> (d e f (:note "de"))))
>
> ;should be converted into this one
> '(a "a"
> (b "abc")
> (d e f "de")))
>
> i.e. recursively find every sublist beginning with :note and replace it with its string
>
>
> I think also subst or subst-if could do such things, but am not able to compose a working function. help appreciated. thank you.

Elias Mårtenson

9/15/2015 3:50:00 AM

0

On Monday, 14 September 2015 22:23:32 UTC+8, His Kennyness wrote:

> Hint: a cons is a data structure with two slots, car and cdr. Either can be
> setf'ed but those of us who wax romantic over paper cuts from Lisp on punch cards
> use rplaca and rplacd.

I've never used punch cards in action, but I still find rplaca and rplacd to
be appealing.

Anyway, can you really get paper cuts from punch cards? They seem to be too thick for that.

Kaz Kylheku

9/15/2015 6:00:00 AM

0

On 2015-09-15, Elias MÃ¥rtenson <lokedhs@gmail.com> wrote:
> On Monday, 14 September 2015 22:23:32 UTC+8, His Kennyness wrote:
>
>> Hint: a cons is a data structure with two slots, car and cdr. Either can be
>> setf'ed but those of us who wax romantic over paper cuts from Lisp on punch cards
>> use rplaca and rplacd.
>
> I've never used punch cards in action, but I still find rplaca and rplacd
> to be appealing.

Punched cards and tape became obsolete near the end of the 1970's when
Usenet started up. These technological relics rapidly gave way to the
convenience and easy availability of ignorant people with poorly considered
ideas and opinions. Which is to say, the punching didn't cease, but
rather continued on a new medium of higher density.

Pascal J. Bourguignon

9/15/2015 6:09:00 AM

0

Kaz Kylheku <kaz@kylheku.com> writes:

> On 2015-09-15, Elias MÃ¥rtenson <lokedhs@gmail.com> wrote:
>> On Monday, 14 September 2015 22:23:32 UTC+8, His Kennyness wrote:
>>
>>> Hint: a cons is a data structure with two slots, car and cdr. Either can be
>>> setf'ed but those of us who wax romantic over paper cuts from Lisp on punch cards
>>> use rplaca and rplacd.
>>
>> I've never used punch cards in action, but I still find rplaca and rplacd
>> to be appealing.
>
> Punched cards and tape became obsolete near the end of the 1970's when
> Usenet started up. These technological relics rapidly gave way to the
> convenience and easy availability of ignorant people with poorly considered
> ideas and opinions. Which is to say, the punching didn't cease, but
> rather continued on a new medium of higher density.

In 1983, they were still heavily used in some places; I had the pleasure
of being taught COBOL on IBM 3031 and on CII-Honeywell-Bull DPS-7 using
punched cards then :-)

Perhaps, one advantage of punched cards that made them last longer than
due, was that the programmers didn't use them directly in general: they
wrote the programs on form paper, and they had girls to punch the
programs. That made for a more mixed work environment :-)


--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

Ivan Kanis

9/15/2015 5:10:00 PM

0

September, 15 at 8:08 Pascal J. wrote:

> Perhaps, one advantage of punched cards that made them last longer than
> due, was that the programmers didn't use them directly in general: they
> wrote the programs on form paper, and they had girls to punch the
> programs. That made for a more mixed work environment :-)

Apparently in Chine programmers have cheerleaders:

http://www.i4u.com/2015/08/94345/chinese-startup-hires-cheerleaders-p...

I hope this news is fake...

--
I know not what course others may take; but as for me, give me
liberty, or give me death!
-- Patrick Henry

Pascal J. Bourguignon

9/16/2015 9:07:00 AM

0

Ivan Kanis <ivan@kanis.fr> writes:

> September, 15 at 8:08 Pascal J. wrote:
>
>> Perhaps, one advantage of punched cards that made them last longer than
>> due, was that the programmers didn't use them directly in general: they
>> wrote the programs on form paper, and they had girls to punch the
>> programs. That made for a more mixed work environment :-)
>
> Apparently in Chine programmers have cheerleaders:
>
> http://www.i4u.com/2015/08/94345/chinese-startup-hires-cheerleaders-p...
>
> I hope this news is fake...

It is not, and it makes programmers from all the world jealous.

--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk