0

I'm attempting to recursively parse through a list that contains numbers, strings, and null lists and convert any strings to numbers using string->number

(define tnode '(5 "5" () ()))

using the function

(define (test funct inlist)
    (cond((null? inlist) '())
         ((number? inlist) ((cons (car inlist) (test funct (cdr inlist)))))
         (else (cons (funct (car inlist)) (test funct (cdr inlist))))
         )
    )

as

(test string->number tnode)

however I am recieving a contract violation error on the first number 5 (and on the later null lists if I use a tnode with only strings and null lists). It seems as if the function is ignoring the first two conditions and going straight to the else statement. Why is this? I dont believe that there are any syntax errors as other cond functions ive tested have worked fine. I'm not quite sure what the issue is.

2 Answers 2

1

inlist is the whole list; you want to be checking the first element of it instead, just as you deal with the first element in your result.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that helped with working around the numbers however the null lists are not appearing. I am getting an output of '(5 5) rather than '(5 5 () ()) as I need.
I have figured out that the problem is, I needed both a condition for (null? inlist) and a condition for (null? (car inlist)) as there would need to be a final null value for it to work due to recurision final code is will be in another answer
0

The funciton code that gave me the desired output was as follows

(define (test funct inlist)
    (cond((null? inlist) '())
         ((null? (car inlist)) (cons '() (test funct (cdr inlist))))
         ((number? (car inlist)) (cons (car inlist) (test funct (cdr inlist))))
         (else (cons (funct (car inlist)) (test funct (cdr inlist))))
         )
    )

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.