Racket/Scheme Flatten Explanations

前端 未结 2 1698
孤城傲影
孤城傲影 2020-12-21 02:40

Can someone help me to break down exactly the order of execution for the following versions of flatten? I\'m using Racket.

version 1, is from racket itself, while v

相关标签:
2条回答
  • 2020-12-21 02:40

    Not really an answer to your question (Chris provided an excellent answer already!), but for completeness' sake here's yet another way to implement flatten, similar to flatten2 but a bit more concise:

    (define (atom? x)
      (and (not (null? x))
           (not (pair? x))))
    
    (define (flatten lst)
      (if (atom? lst)
          (list lst)
          (apply append (map flatten lst))))
    

    And another way to implement the left-fold version (with more in common to flatten1), using standard Racket procedures:

    (define (flatten lst)
      (define (loop lst acc)
        (if (atom? lst)
            (cons lst acc)
            (foldl loop acc lst)))
      (reverse (loop lst '())))
    
    0 讨论(0)
  • 2020-12-21 02:54

    The main difference is this:

    • flatten1 works by storing the output elements (first from the cdr side, then from the car side) into an accumulator. This works because lists are built from right to left, so working on the cdr side first is correct.
    • flatten2 works by recursively flattening the car and cdr sides, then appending them together.

    flatten1 is faster, especially if the tree is heavy on the car side: the use of an accumulator means that there is no extra list copying, no matter what. Whereas, the append call in flatten2 causes the left-hand side of the append to be copied, which means lots of extra list copying if the tree is heavy on the car side.

    So in summary, I would consider flatten2 a beginner's implementation of flatten, and flatten1 a more polished, professional version. See also my implementation of flatten, which works using the same principles as flatten1, but using a left-fold instead of the right-fold that flatten1 uses.

    (A left-fold solution uses less stack space but potentially more heap space. A right-fold solution uses more stack and usually less heap, though a quick read of flatten1 suggests in this case that the heap usage is about the same as my implementation.)

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