CPSC 110 Lecture Notes - Lecture 22: Arity, Binary Tree, Tail Call

169 views11 pages
Verified Note
19 Nov 2018
School
Course
Professor

Document Summary

(require spd/tags) (@htdd tree) (define-struct node (name subs)) [else ( (fn-for-t (first lot)) (fn-for-lot (rest lot)))]))] (fn-for-t t))) ;; todo is (local [(define (fn-for-t t todo acc) ;( (node-name t) (fn-for-lot (append (node-subs t) todo) ( acc))) (define (fn-for-lot todo acc) (cond [(empty? todo) ( acc)] [else (fn-for-t (first todo) (rest todo) ( acc))]))] ; (fn-for-lot (rest lot)))]))] (fn-for-t t ))) ;; refactor the function design provided below so that it is tail-recursive. (@htdf all-names) (@signature tree -> (listof string)) [else (append (fn-for-t (first lot)) (fn-for-lot (rest lot)))]))] (fn-for-t t))) (@template tree (listof tree) accumulator) (define (all-names t) ;; rsf is (listof string): names of nodes visited so far. ;; todo is (listof tree): worklist accumulator of nodes needed to be visited (local [(define (fn-for-t t todo rsf) (fn-for-lot (append (node-subs t) todo) (cons (node-name t) rsf))) (define (fn-for-lot todo rsf) (cond [(empty? todo) (reverse rsf)]

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents