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

HELP with a small Function ....

8 views
Skip to first unread message

Robert L.

unread,
May 24, 2018, 3:51:50 PM5/24/18
to
Stig Hemmer wrote:

> > For example if the list '( a b + c d + * )
> > is passed in the list ( ( a + b ) * ( c + d ) )
> > or if the list '( a b c d e f g + - + - + - )
> > is passed then ( a - ( b + ( c - ( d + ( e - ( f + g ) )))))
> ...
> > The function cannot use any iterative constructs like ( prog , do or loop ).
>
> (defun transmogrify (postfix)
> (let ((stack ()))
> (loop for element in postfix
> if (member element '(+ * - /))
> do (push (reverse (list (pop stack) element (pop stack))) stack)
> else
> do (push element stack))
> (first stack)))
>
> Replacing the LOOP is left as an exercise for the reader.

(define (opcons op lst)
`((,(cadr lst) ,op ,(car lst)) ,@(cddr lst)))

(define (trans postfix)
(car
(fold
(lambda (x accum) ((if (member x '(+ - * /)) opcons cons) x accum))
'()
postfix)))

> (trans '( a b + c d + * ))
'((a + b) * (c + d))

> (trans '( a b c d e f g + - + - + - ))
'(a - (b + (c - (d + (e - (f + g))))))

--
"They created ... an America which is not the real America ... but ultimately
this shadow America becomes ... so widely disseminated that its images and its
values come to devour the real America. And so the grand irony of all of
Hollywood is that Americans come to define themselves by the shadow of
America...." http://archive.org/details/nolies
0 new messages