Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;https://stackoverflow.com/questions/62946821/
- ;;scheme-is-it-possible-to-convert-a-list-of-s-expressions-
- ;;into-a-list-of-atoms/62959036#62959036
- ;;by https://stackoverflow.com/users/5920214/tfb,
- ;;tweaked by https://stackoverflow.com/users/849891/will-ness,
- ;;removing the tailrec-accum-and-reverse
- (define (tree->atoms tree)
- (define (atom? x) (not (pair? x)))
- (let tree->atoms-loop ([it tree]
- [agenda '()])
- (cond [(null? it)
- ;; no more left
- (if (null? agenda)
- ;; no more agenda: we're done
- '()
- ;; more agenda, so carry on
- (tree->atoms-loop (first agenda)
- (rest agenda)))]
- [(atom? it)
- ;; we've found an atom which is not ()
- (if (null? agenda)
- ;; no more agenda: we're done
- (list it)
- ;; more agenda, so carry on
- (cons it (tree->atoms-loop (first agenda)
- (rest agenda))))]
- [else ; (not (atom? it))
- ;; cons: look at the car, and stuff the cdr onto the agenda
- (tree->atoms-loop (car it)
- (cons (cdr it) agenda))])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement