reverse list - scheme

前端 未结 8 1919
清酒与你
清酒与你 2020-12-11 05:46

I\'m trying to reverse a list, here\'s my code:

(define (reverse list)
  (if (null? list) 
     list
      (list (reverse (cdr list)) (car list))))


        
相关标签:
8条回答
  • 2020-12-11 06:29

    Here is a recursive procedure that describes an iterative process (tail recursive) of reversing a list in Scheme

    (define (reverse lst)
      (define (go lst tail)
        (if (null? lst) tail
            (go (cdr lst) (cons (car lst) tail))))
      (go lst ())))
    

    Using substitution model for (reverse (list 1 2 3 4))

    ;; (reverse (list 1 2 3 4))                                                                                                                           
    ;; (go (list 1 2 3 4) ())                                                                                                                             
    ;; (go (list 2 3 4) (list 1))                                                                                                                         
    ;; (go (list 3 4) (list 2 1))                                                                                                                         
    ;; (go (list 4) (list 3 2 1))                                                                                                                         
    ;; (go () (list 4 3 2 1))                                                                                                                             
    ;; (list 4 3 2 1)
    

    Here is a recursive procedure that describes a recursive process (not tail recursive) of reversing a list in Scheme

    (define (reverse2 lst)
      (if (null? lst) ()
        (append (reverse2 (cdr lst)) (list (car lst)))))
    
    (define (append l1 l2)
      (if (null? l1) l2
          (cons (car l1) (append (cdr l1) l2))))
    

    Using substitution model for (reverse2 (list 1 2 3 4))

    ;; (reverse2 (list 1 2 3 4))                                                                                                                          
    ;; (append (reverse2 (list 2 3 4)) (list 1))                                                                                                          
    ;; (append (append (reverse2 (list 3 4)) (list 2)) (list 1))                                                                                          
    ;; (append (append (append (reverse2 (list 4)) (list 3)) (list 2)) (list 1))                                                                          
    ;; (append (append (append (append (reverse2 ()) (list 4)) (list 3)) (list 2)) (list 1))                                                              
    ;; (append (append (append (append () (list 4)) (list 3)) (list 2)) (list 1))                                                                         
    ;; (append (append (append (list 4) (list 3)) (list 2)) (list 1))                                                                                     
    ;; (append (append (list 4 3) (list 2)) (list 1))                                                                                                     
    ;; (append (list 4 3 2) (list 1))                                                                                                                     
    ;; (list 4 3 2 1)
    
    0 讨论(0)
  • 2020-12-11 06:30
    (define reverse?
      (lambda (l)
        (define reverse-aux?
          (lambda (l col)
            (cond 
              ((null? l) (col ))
              (else 
                (reverse-aux? (cdr l) 
                              (lambda () 
                                (cons (car l) (col))))))))
        (reverse-aux? l (lambda () (quote ())))))
    (reverse? '(1 2 3 4) )
    

    One more answer similar to Oscar's. I have just started learning scheme, so excuse me in case you find issues :).

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