FRUSTRATION WITH SCHEME!!!

SnoopCat

Senior member
Jan 19, 2001
926
0
0
(define my-append
(lambda (L1 L2)
(cond
((null? L2) '())
(else (cons L1 (car (my-append L1 (cdr L2)))))
)
)
)



its supposed to

(my-append L1 L2) takes two lists L1 and L2 and returns the concatenation of L1 to the end of L2. Note that concatenating two lists is not the same as "consing" two lists together!

(my-append '() '(a b)) => (a b)
(my-append '(a b) '()) => (a b)
(my-append '(c d) '(a b)) => (a b c d)



i have no clue how to start this!!!!!!!!!!!!!!!!!!!!! been lookking at thise for 2 hrs!!!
 

dabuddha

Lifer
Apr 10, 2000
19,579
17
81
hehe i feel your pain. It's been 2 years since i had to use scheme and i don't miss it at all
 

BCYL

Diamond Member
Jun 7, 2000
7,803
0
71
haha... I thought my school is the only one who teaches Scheme... now I know I am not alone... :)
 

ElPool

Senior member
Oct 11, 2000
665
0
0
hehe. im in a stupid intro scheme class right now too.
lemme see.
so you have two lists. l1 and l2, say '(c d) '(a b)
you just need to add A and B to the FRONT of (c d)

(cond

( (null? l2) l1 )
(else (cons (car l2) (my-append (cdr l2)) ) )

maybe something like that? each recursion add one of the elements in l2
to the front of l1.

hmm. actually i think this might give you '(b a c d) :(
theres always the 'reverse' primitive function.

 

ggavinmoss

Diamond Member
Apr 20, 2001
4,798
1
0
One solution would be this:

You can get the last element in a list by using cdddr (this is in some implementations -- and is only shorthand anyway). Next, define an auxiliary function, say 'cdr-x', that is similar to 'cdr', but it returns the list without the last element (whereas 'cdr' returns the rest of the list without the *first* element).

Then, it *should* work like this:

(define
(append-x a b)
(cond
((null? b) a)
(else (append-x (cons (cdddr b) a) (cdr-x b)))
)
)

Although I could just be talking out my a$$. And let me add that I hate scheme.
:)

-geoff


)