The original code I try to implement.. the output should be (1.4) (2.5) from my code.. I think you all know what I try to do....this is also tail recursion practice
my code
(define (myFunc lst1 lst2)
(if (or (null? lst1) (null? lst2))
'()
(list (cons (car lst1) (car lst2))
(myFunc (cdr lst1) (cdr lst2)))
))
after several of you gave me good advice about cons-pair.. so now it get's the dotted symbol in the middle.. then problem is that the improper list with empty list in the end.. when 2 input lists are like this ..... '(1 2 3 4) '(4 5 6)) my output is like this ; ((1 . 4) ((2 . 5) ((3 . 6) ()))) the empty list in the end of output shouldn't be there... so I couldn't understand about improper list , proper list....? is there are any document, I can look at?
Consider the difference between cons
and list
:
That is, (cons a b)
creates a cell whose car
is a
and cdr
is b
.
(list a b)
creates a cell whose car
is a
, but the cdr
is a list, and the car
of that list is b
, while its cdr
is nil
.
If b
is a list, the one on the left will be a list which has b
as its tail, and with a
added at the front of b
.
The one on the right will also be a list, but one which has b
as its second element, not as its tail like you want.
To fix your program, you only need to replace your list
with a cons
.
But your function is not tail-recursive, because it does things with the result of the recursive call.
To make it tail-recursive, a good way is usually to make a helper function which has an accumulator parameter.
I would probably write it something like this:
(define (zip-cars l1 l2)
(cons (car l1) (car l2)))
(define (zip-help l1 l2 result)
(if (or (null? l1) (null? l2))
result
(zip-help (cdr l1) (cdr l2) (cons (zip-cars l1 l2) result))))
(define (zip l1 l2)
(zip-help l1 l2 '()))