Reputation: 8577
I have the following code:
(define rest '(((di (a) (b c)) (sh (b) (e d))) ((al (a) (b)))))
(define first '((di (a) (5)) (sh (b) (3))))
I want to get the following list:
(((di (a) (5)) (sh (b) (3))) ((di (a) (b c)) (sh (b) (e d))) ((al (a) (b)))))
meaning, add the list first
, to be the first element in rest.
When I do append
, it gives me:
((di (a) (5)) (sh (b) (3)) ((di (a) (b c)) (sh (b) (e d))) ((al (a) (b))))
And any other library function, or function that I try to do, didn't help.
Thank you.
Upvotes: 6
Views: 10630
Reputation: 236004
It's not a good idea to use first
and rest
as names for your lists, since they're predefined procedures in some versions of scheme (Racket, for instance) and you'd be redefining them.
A simple cons
will do the trick, as you can see below:
(define fst '((di (a) (5)) (sh (b) (3))))
(define rst '(((di (a) (b c)) (sh (b) (e d))) ((al (a) (b)))))
(cons fst rst)
> (((di (a) (5)) (sh (b) (3))) ((di (a) (b c)) (sh (b) (e d))) ((al (a) (b))))
It works because the list you want to obtain is simply rst
but with a new element, fst
(a list) at the beginning, whereas you'd typically use append
when you want to add the elements of one list at the end of another list.
Upvotes: 2
Reputation: 68152
Append
takes two lists and puts them together. Given that you have a first
and a rest
, you probably want cons
. Cons
takes an element and prepends it to a list. In this case, the element is first
and the list is rest
. So you want something like
(cons first rest)
Upvotes: 7