How to Reverse a List?

前端 未结 7 2077
有刺的猬
有刺的猬 2020-12-03 06:28

What is the function to a list in Scheme? It needs to be able to handle nested lists.

So that if you do something like (reverse \'(a (b c d) e)) you\'ll

相关标签:
7条回答
  • 2020-12-03 07:00

    This is one way that you can make a reverse function that applies to nested lists:

    (define (reverse-deep l)
      (map (lambda (x) (if (list? x) (reverse-deep x) x)) (reverse l)))
    

    Explanation in pseudo-code:
    Start by reversing the list as you would normally do
    Then for each element in the reversed list:
    - If the element is a list itself: Apply the procedure recursively
    - Else: Don't touch the element

    0 讨论(0)
提交回复
热议问题