Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

recursive problem

0 views
Skip to first unread message

Christian Drescher

unread,
Dec 2, 2004, 4:47:03 PM12/2/04
to
(DEFUN rebuild (tree)
(COND
((ATOM tree) tree)
(T (APPEND (CONS (cadr tree) NIL)
(CONS (rebuild (car tree)) NIL)
(rebuild (cddr tree)) )) ))

</code>

I try to rebuild an infix noted list to an prefix noted like this:

First, I get arithmetik input this way:
(1 + (2 - 1))

Now, I want it evaluable:
(+ 1 (- 2 1))

But there is a problem with CDDR ... (CDDR tree) returns a list everytime,
so that the function returns:
(+ 1 NIL (- 2 NIL 1))

Help, please.


Message has been deleted

Thomas A. Russ

unread,
Dec 2, 2004, 5:19:34 PM12/2/04
to
"Christian Drescher" <christ...@gmx.net> writes:

>
> (DEFUN rebuild (tree)
> (COND
> ((ATOM tree) tree)
> (T (APPEND (CONS (cadr tree) NIL)
> (CONS (rebuild (car tree)) NIL)
> (rebuild (cddr tree)) )) ))
>
> </code>
>
> I try to rebuild an infix noted list to an prefix noted like this:
>
> First, I get arithmetik input this way:
> (1 + (2 - 1))
>
> Now, I want it evaluable:
> (+ 1 (- 2 1))
>
> But there is a problem with CDDR ... (CDDR tree) returns a list everytime,

just like it is supposed to. Remeber that

(CDDR x) <=> (CDR (CDR x))

> so that the function returns:
> (+ 1 NIL (- 2 NIL 1))
>
> Help, please.

You probably want CADDR.

or better yet, you probably want to use SECOND, FIRST and THIRD :)

--
Thomas A. Russ, USC/Information Sciences Institute


David Sletten

unread,
Dec 2, 2004, 6:27:32 PM12/2/04
to
Christian Drescher wrote:

This is homework?

You are dealing with lists of the form: (<expr> <op> <expr>) where each
of the operands may itself be a list. So you are right, this potentially
requires recursive processing, but not as much you think. There are only
3 top-level elements in your list. You need to process the FIRST and
THIRD (hint), but the SECOND just gets moved.

The preceding assumes that your expressions are fully parenthesized:
(rebuild '(((3 * 5) + 9) - 1)) => (- (+ (* 3 5) 9) 1)
If this is not the case:
(rebuild '(3 * 5 + 9 - 1))
then you have more work to do.

David Sletten

Christian Drescher

unread,
Dec 3, 2004, 8:45:06 AM12/3/04
to

"David Sletten" <da...@slytobias.com> schrieb im Newsbeitrag
news:ExNrd.2631$nP1....@twister.socal.rr.com...

Yeah, my expressions are fully parenthesized. I will fix my work later, but
I know - thanks to you - THIRD or rather CADDR should be the solution.

Thanks.


0 new messages