I have just started learning scheme, and I find the cons-cdr part a bit hard to understand. I am making a function which takes a list, then displays all the atoms in that list, including in the sub lists as if it were one big list. It would look like this: (flatten '(1 (2 3) 4 5 (6 7)))
(1 2 3 4 5 6 7)
Here is my code:
(define (flatten list1)
(if (not (empty? list1))
(if (atom? (car list1))
(cons (car list1)(flatten (cdr list1)))
(begin
(flatten (car list1))
(flatten (cdr list1))))
'()))
However, when doing this, it removes the sublists. So (flatten '((1 2) 3 4) would give (3 4) instead of (1 2 3 4).
Any help? The problem is probably in the "(begin" section, but i can't figure it out..
Thanks!