/ / Scal w schemacie - rekurencja, scalanie, schemat

Scal w schemacie - rekurencja, scalenie, schemat

Próbuję połączyć dwie listy w schemacie i uruchamiać błąd #void z moim kodem

    (define x "(1 2 3 4 5 6 7 8 15 10))
(define z "(11 12 13))

(define merge
(lambda (list1 list2)
(cond [(null? list1) list2] ;if first list is empty return second list
[(null? list2) list1] ;if second list is empty return first list
[(if (< (car list1) (car list2))
(cons (car list1) (merge (cdr list1) list2)))] ;if 1st item in 1st list is less than 1st item in second list (cons 1st item from 1st list with merge call)
[(if (> (car list1) (car list2))
(cons (car list2) (merge (cdr list2) list1)))] ;if 1st item in 1st list is greater than 1st item in second list (cons 1st item in the 2nd list with merge call)
)))

wyjścia kończą się pustką w scaleniu

        > (merge x z)
(1 2 3 4 5 6 7 8 . #<void>)
>

Odpowiedzi:

1 dla odpowiedzi № 1

Ty masz if oświadczenia w twoim cond i brakowało else klauzula.

(define x "(1 2 3 4 5 6 7 8 15 10))
(define z "(11 12 13))

(define merge
(lambda (list1 list2)
(cond [(null? list1) list2] ;if first list is empty return second list
[(null? list2) list1] ;if second list is empty return first list
[(< (car list1) (car list2))
(cons (car list1) (merge (cdr list1) list2))] ;if 1st item in 1st list is less than 1st item in second list (cons 1st item from 1st list with merge call)
[(> (car list1) (car list2))
(cons (car list2) (merge (cdr list2) list1))] ;if 1st item in 1st list is greater than 1st item in second list (cons 1st item in the 2nd list with merge call)
[else (cons (car list1) (cons (car list2) (merge (cdr list1) (cdr list2))))]
)))

Zobaczyć, że:

> (merge x z)
"(1 2 3 4 5 6 7 8 11 12 13 15 10)

The #<void> przepadło.