How to construct a tree of particular shape with elements from a list

Jacob

Given an s-expression '((a . b) . (c . d)) and a list '(e f g h), how can I traverse the s-expression create an s-expression with the same shape, but with elements taken from the list? E.g., for the s-expression and list above, the result would be '((e . f) g . h)?

Joshua Taylor

Traversing a tree of pairs in left to right order isn't particularly difficult, as car and cdr let you get to both sides, and cons can put things back together. The tricky part in a problem like this is that to "replace" elements in the right hand side of a tree, you need to know how many of the available inputs you used when processing the left hand side of the tree. So, here's a procedure reshape that takes a template (a tree with the shape that you want) and a list of elements to use in the new tree. It returns as multiple values the new tree and any remaining elements from the list. This means that in the recursive calls for a pair, you can easily obtain both the new left and right subtrees, along with the remaining elements.

(define (reshape template list)
  ;; Creates a tree shaped like TEMPLATE, but with 
  ;; elements taken from LIST.  Returns two values: 
  ;; the new tree, and a list of any remaining
  ;; elements from LIST.
  (if (not (pair? template))
      (values (first list) (rest list))
      (let-values (((left list) (reshape (car template) list)))
        (let-values (((right list) (reshape (cdr template) list)))
          (values (cons left right) list)))))

(reshape '((a . b) . (c . d)) '(e f g h))
;=> ((e . f) g . h)
;=> ()

(reshape '((a . b) . (c . d)) '(e f g h i j k))
;=> ((e . f) g . h)
;=> (i j k)          ; leftovers

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to delete particular no of elements from array in mongodb

分類Dev

How can I join list elements into particular other elements

分類Dev

How to delete some elements from list in kotlin

分類Dev

How to select certain elements from a list?

分類Dev

How to remove zero elements from List<string>?

分類Dev

How to recursively combine pairs of elements from a list?

分類Dev

How to take only unique elements from a list?

分類Dev

Construct pandas dataframe from nested list

分類Dev

How to select elements in jQuery that are not children of particular element?

分類Dev

how to find elements from a list that are not present in another list in r

分類Dev

Taking Elements From a List

分類Dev

scheme consumes BT tree and returns the elements of the tree as a list of strings

分類Dev

How to take x many items to the left of a particular index from a list, including 0 many items

分類Dev

How to list all git branches created from any commit of a particular branch

分類Dev

How do I list the files having particular strings from group of directories in bash?

分類Dev

How to create new list from list of list where elements are in new list are in alternative order?

分類Dev

How to view list of backup files on a particular date

分類Dev

How to list packages with files in a particular directory?

分類Dev

How to print a element of list in a particular format

分類Dev

How to list characters of a particular frequency in a string

分類Dev

How to get the absolute url of img elements from an unordered list in JSoup

分類Dev

How to retrieve elements that have the same foreign key from a list?

分類Dev

How to construct XPath from variables in Selenium?

分類Dev

Python remove duplicate elements from xml tree

分類Dev

create tree from list of list c#

分類Dev

How to list the kernel Device Tree

分類Dev

How to list a process tree on Windows?

分類Dev

Construct a dictionary from a list of dictionary keys, sub-keys and values

分類Dev

Construct dataframe from pairwise Word Mover Distance score list

Related 関連記事

  1. 1

    How to delete particular no of elements from array in mongodb

  2. 2

    How can I join list elements into particular other elements

  3. 3

    How to delete some elements from list in kotlin

  4. 4

    How to select certain elements from a list?

  5. 5

    How to remove zero elements from List<string>?

  6. 6

    How to recursively combine pairs of elements from a list?

  7. 7

    How to take only unique elements from a list?

  8. 8

    Construct pandas dataframe from nested list

  9. 9

    How to select elements in jQuery that are not children of particular element?

  10. 10

    how to find elements from a list that are not present in another list in r

  11. 11

    Taking Elements From a List

  12. 12

    scheme consumes BT tree and returns the elements of the tree as a list of strings

  13. 13

    How to take x many items to the left of a particular index from a list, including 0 many items

  14. 14

    How to list all git branches created from any commit of a particular branch

  15. 15

    How do I list the files having particular strings from group of directories in bash?

  16. 16

    How to create new list from list of list where elements are in new list are in alternative order?

  17. 17

    How to view list of backup files on a particular date

  18. 18

    How to list packages with files in a particular directory?

  19. 19

    How to print a element of list in a particular format

  20. 20

    How to list characters of a particular frequency in a string

  21. 21

    How to get the absolute url of img elements from an unordered list in JSoup

  22. 22

    How to retrieve elements that have the same foreign key from a list?

  23. 23

    How to construct XPath from variables in Selenium?

  24. 24

    Python remove duplicate elements from xml tree

  25. 25

    create tree from list of list c#

  26. 26

    How to list the kernel Device Tree

  27. 27

    How to list a process tree on Windows?

  28. 28

    Construct a dictionary from a list of dictionary keys, sub-keys and values

  29. 29

    Construct dataframe from pairwise Word Mover Distance score list

ホットタグ

アーカイブ