Reputation: 1954
Calling all racket developers, I am a newbie to racket language and function languages in general. Long story short, I have a nested list that contains numbers and string and I want to convert string elements to numbers.
Example: If I have list like this
'( 3 "3"( 1 "1"()( 2 "2" () ()))( 5 "5" () () ))
'( 3 3( 1 1()( 2 2 () ()))( 5 5 () () ))
(define (mapBst BST someFunct )
(if (null? BST) '()
(someFunct (car(cdr BST)) (mapBST someFunct (cdr (car BST))))
)
)
(mapBst '( 3 "3"( 1 "1"()( 2 "2" () ()))( 5 "5" () () )) string->number)
But I am getting this error : cdr: contract violation expected: pair? given: 3
Any clue why I am getting this error or what I did wrong, any suggestions will help. Thank you in advance
Upvotes: 0
Views: 1714
Reputation: 679
I suggest this:
(define (mapBST BST someFunct)
(cond [(null? BST)
'()]
[(list? (car BST))
(cons (mapBST (car BST) someFunct)
(mapBST (cdr BST) someFunct))]
[else
(cons (someFunct (car BST))
(mapBST (cdr BST) someFunct))]))
Example:
> (mapBST '( 3 "3"( 1 "1"()( 2 "2" () ()))( 5 "5" () () ))
(λ (x) (if (string? x) (string->number x) x)))
'(3 3 (1 1 () (2 2 () ())) (5 5 () ()))
Upvotes: 1