I'm stuck with a little problem here.
Say that I have a list of lists such as:
(define x '(("1" "one" "two" "2" "three")
("2" "four" "five" "0" "six")
("3" "seven" "eight" "4" "nine")
("4" "ten" "eleven" "2" "twelve")))
So what I want to do is to get each of those lists and make the elements within each one become a single string, such as:
"1 one two 2 three"
"2 four five 0 six" ...
I have designed these functions:
(define (get-sublista-from-lista lista)
(if (list? (car lista))
(car lista)
lista))
and:
(define (string-from-lista lista)
(define (list-to-string lista str)
(if (null? (cdr lista))
(string-append str (car lista))
(list-to-string (cdr lista)
(string-append str (car lista) " "))))
(list-to-string lista ""))
which work properly by themselves. But whenever I try a combination of the two of them or of one of them with another, like in:
(define (string-from-lista1 lista)
(define (list-to-string lista str)
(if (null? (get-sublista-from-lista lista))
(string-append str (car (get-sublista-from-lista lista)))
(list-to-string (cdr (get-sublista-from-lista lista))
(string-append str (car (get-sublista-from-lista lista)) ""))))
(list-to-string lista ""))
Then the individual lists within the master lists are passed as '(), resulting in a contract violation such as:
car: contract violation
expected: pair?
given: '()
Can someone explain why am I getting such error?
A built-in function string-join
works to join different strings in a list into one string separated by default character space. Use map
to apply it all members of list x and you will have a short code to accomplish this task:
(map string-join x)
Output:
'("1 one two 2 three" "2 four five 0 six" "3 seven eight 4 nine" "4 ten eleven 2 twelve")