0

I am not sure how to convert a list to a string within a function when I recursively call it. For example, my code is as follows:

(define (pre-order T)
 (define (conv x)
    (cond ((number? x) (number->string x))
    ((char? x) (string x))))

(map-diff (lambda (x) (conv x)) T)

   (cond ((null? T) "")
    ((eq? (value T) #\+)
           (cons "+" (pre-order (right T))(pre-order (left T))))
     ((eq? (value T) #\*)
           (cons "*" (pre-order (right T))(pre-order (left T))))
     ((eq? (value T) #\-)
           (cons "-" (pre-order (left T))))
     ((eq? (value T) #\/)
           (cons "/" (pre-order (left T))))
     (else (cons (value T) (pre-order (left T))))))

Assume that my map function works properly for this. I am working with trees and would like also like to know how to write an arithmetic parse tree in prefix notation. I have done my research on arithmetic parse trees, and I wrote a function that can calculate their value, but I don't know how to traverse them properly for this problem.

1
  • Just answer my question about strings here, I am making a new post about preorder traversals. Oct 19, 2013 at 22:37

1 Answer 1

1

A String is a sequence of characters. A list of characters can easily be converted to a string.

(define charlst '(#\H #\e #\l #\l #\o))
(list->string charlst); ==> "Hello"

If you have a list of strings you can string-append them:

(define stringlst '("A" "B" "C"))
(apply string-append stringlst) ; ==> "ABC"

Since it seems you're working with characters it would be easier just using them in the list and list->string the result since it's a effortless task for compared to string-append which is similar to appending lists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.