mr nooby noob
mr nooby noob

Reputation: 2253

Scheme: How to convert a charlist to a string

I am stumped. I am trying to convert a charlist back to a string but it keeps failing: Its stupid because when I paste the result from one function into another it works just fine, but if I do it inside the function it fails...

Example: this works

(delete" h ell o ") outputs: '(#\h #\e #\l #\l #\o) (convertToString '(#\h #\e #\l #\l #\o)) outputs: "hello"

but this doesnt work, if the conertToString is called in delete this happens

(delete" hell o") outputs:. . list->string: contract violation expected: (listof char?) given: '(#\l . "o")

(define deleteCh;driver
  (lambda (s)
    (delete(string->list s))))

(define delete 
  (lambda (input)
    (cond
      [(null? input) input]
      [(equal? (car input) #\space)  (delete (cdr input))]
      [else (convertToString (cons (car input) (delete (cdr input))))])));this works without convertToString 

(define convertToString
  (lambda (charList)
    (list->string charList)))

Upvotes: 0

Views: 552

Answers (1)

Sylwester
Sylwester

Reputation: 48735

Your problem is with delete. For every char that is not a space you do convertToString with the result. Thus with the string "abc" it will do

(convertToString (cons #\a (convertToString (cons #\b (convertToString (cons #\c '()))))))

Hint.. You need to do convertToString (funny wrapper for list->string) with the result in deleteCh instead of every subresult.

Upvotes: 1

Related Questions