I've just started to learn Racket and I need to create a procedure that merge two list randomly.
This is my code:
#lang racket
(define merge-randomly
(lambda (lst1 lst2)
(cond ((and (null? lst1) (null? lst2)) null)
((null? lst1) lst2)
((null? lst2) lst1)
(else
(cond ((> (random) 0.5) (cons (cons (car lst1) (car lst2)) (merge-randomly (cdr lst1) (cdr lst2))))
(else (cons (cons (car lst2) (car lst1)) (merge-randomly (cdr lst1) (cdr lst2))))
)
)
)
)
)
I need to use with list like these two:
(define l1 '((1 2 3) (7 8 9)))
(define l2 '((4 5 6)))
I need to create a new list like this one:
'((4 5 6) (1 2 3) (7 8 9))
But I get this list:
'(((4 5 6) 1 2 3) (7 8 9))
I'm doing something wrong, probably here, (cons (cons (car lst1) (car lst2))
, or here, (cons (cons (car lst2) (car lst1))
, at the else instruction in first cond
.
How can I get the list I want to get?