[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: Reversing nested lists

William James

3/8/2016 8:39:00 AM

Fred Gilham wrote:

> > I'm writing a function that should reverse a nested list. For
> > example:
> >
> > (reverse-list '(a b (c d (e) f (g (h i)) j) k))
> >
> > should return:
> >
> > (K (J ((I H) G) F (E) D C) B A)
> >
> > It's an assignment so I'm not looking for complete code, that would
> > be cheating and I enjoy figuring out these things myself. I would be
> > happy just to get some pointers on how I should go about writing
> > this double-recursively. Just some pseudo-code. It would help loads.
>
> Since I note that you already posted your code, here's my solution:
>
> (defun reverse-list (thing)
> (cond ((atom thing) thing)
> (t (append (reverse-list (cdr thing))
> (list (reverse-list (car thing)))))))
>
> CL-USER> (reverse-list '(a b (c d (e) f (g (h i)) j) k))
> (K (J ((I H) G) F (E) D C) B A)
> CL-USER> (reverse-list '((a b (c d (e) f (g (h i)) j) k)))
> ((K (J ((I H) G) F (E) D C) B A))


MatzLisp (Ruby):

def reverse_all array
array.map{|x| x.is_a?(Array) ? reverse_all(x) : x}.reverse
end

reverse [:a,:b,[:c,:d,[:e],:f,[:g,[:h,:i]],:j],:k]
==>[:k, [:j, [[:i, :h], :g], :f, [:e], :d, :c], :b, :a]


--
[T]he number of Jews killed there was 800,000,000.... [T]he Talmud makes it all
clear by informing us that the blood of the holocausted Jews ran to the sea in
a huge tidal wave that swept boulders in its path and was so deep that it
reached the nostrils of the Romans' horses.
nationalvanguard.org/2014/09/disillusioned-part-1/