[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: HELP with a small Function ....

William James

1/24/2016 8:50:00 AM

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.

MatzLisp (Ruby):

def parse_postfix tokens
stack = []
tokens.each{|tok|
stack.push(if [:+,:*,:-,:/].include? tok
[stack.pop,tok,stack.pop].reverse
else
tok
end)}
stack.pop
end

parse_postfix [:a,:b,:+,:c,:d,:+,:*]
===>
[[:a, :+, :b], :*, [:c, :+, :d]]

parse_postfix [:a, :b, :c, :d, :e, :f, :g, :+, :-, :+, :-, :+, :-]
===>
[:a, :-, [:b, :+, [:c, :-, [:d, :+, [:e, :-, [:f, :+, :g]]]]]]

--
[A] brutal gang rape ... was reported ... thus: "Several Swedish Men Suspected
of Rape" (Dagens Nyheter). "Six Swedish Men Raped Woman" (Aftonbladet). "Six
Swedes Arrested for Rape" (Expressen).... All the suspects were Muslim, and
all were foreign nationals, having no Swedish citizenship.
thenewamerican.com/world-news/europe/item/20142-cover-up-the-swedish-left-s-sacrifice-of-women-to-political-correctness