如何做一个 return 树的所有分支作为列表中的字符串的函数
How to do a function that return all the tree's branch as string in a list
例如在这种情况下它应该 return ("casa" "caso "cal" "cola" "coma" "cena").
我已经这样做了,尝试使用 tail 的递归:
(define (palabras-tree tree)
(palabras-tree-aux '() tree))
(define (palabras-tree-aux l tree)
(if (leaf? tree)
(cons (symbol->string (root tree)) l)
(cons (symbol->string(root tree))
(fold-right append '() (map (lambda (t)
(string-append (symbol->string (root t)))
(palabras-tree-aux l t)) (children tree))))))
但它 return 是这样的:{c a s a o l o l a m o e n a}
感谢您的帮助!
这是一个可能的实现,没有尾递归(使用 DrRacket 测试):
(define (all-names tree)
(let ((first-char (symbol->string (root tree))))
(if (leaf? tree)
(list first-char)
(map (lambda (el) (string-append first-char el))
(append-map all-names (children tree))))))
例如在这种情况下它应该 return ("casa" "caso "cal" "cola" "coma" "cena").
我已经这样做了,尝试使用 tail 的递归:
(define (palabras-tree tree)
(palabras-tree-aux '() tree))
(define (palabras-tree-aux l tree)
(if (leaf? tree)
(cons (symbol->string (root tree)) l)
(cons (symbol->string(root tree))
(fold-right append '() (map (lambda (t)
(string-append (symbol->string (root t)))
(palabras-tree-aux l t)) (children tree))))))
但它 return 是这样的:{c a s a o l o l a m o e n a}
感谢您的帮助!
这是一个可能的实现,没有尾递归(使用 DrRacket 测试):
(define (all-names tree)
(let ((first-char (symbol->string (root tree))))
(if (leaf? tree)
(list first-char)
(map (lambda (el) (string-append first-char el))
(append-map all-names (children tree))))))