开发者

Scheme Deep reverse function

开发者 https://www.devze.com 2023-01-28 07:50 出处:网络
I am doing a scheme program which takes in a list and then reverse it. So far it works for simple list, which does not contain any sublist, but when I test it for开发者_运维问答 a list contains sublis

I am doing a scheme program which takes in a list and then reverse it. So far it works for simple list, which does not contain any sublist, but when I test it for开发者_运维问答 a list contains sublist, it fails. Please help me where is wrong with it. Here is the code:

(define deep-reverse
  (lambda (L)
    (cond
      ((empty? L) '())
      (else (append (deep-reverse (rest L)) (list (first L)))))))


(define (deeprev L)
          (if (null? L) '()
              (if (list? (car L))
                  (if (chek (car L)) (append (deeprev (cdr L)) (list (reverse (car L))))         
                  (append (deeprev (cdr L)) (list (deeprev (car L)))))
                  (append (deeprev (cdr L)) (list (car L))))))


First of all, you are using undefined Scheme functions. I am going to work off the following assumptions:

empty? is null?

rest is cdr

first is car

Your code works by taking the first element in a list and adding it to another list. However, that first element of a list can be a list itself. You need to test to see if the element that you're working on is atomic or a list. If it's a list, then you call deep-reverse recursively.

If you would like to see code appended to this, leave a comment.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号