</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.
>
> (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
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
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.