0

So, I have a school project in Scheme (working in Dr.Racket enviorment), here is a short run what it is (please note that im not asking any of you to do my schoolwork for me).

I have 2 lists : one is 6 or more characters and the other one is 3 characters.

What we have to dois to combine them in a way that will look like this:

List 1: (1 2 3 4 5 6 7 8)
List 2: (a b c)

To this:

(1 a 2 b 3 c 4 5 c 6 b 7 a 8)

Now, I have an idea in mind on how to do this, which involves "cutting" it to parts and using cons to put them back togther, after using cons to "attach" the letters.

Here`s my problem though: I made a function thats supposed to construct 2 lists into one, but its not working and gives me the names that i used in the function define.

Here is the code:

(define (match List1 List2)
(cons (List1) (List2)))
(match (1 2 3) (5 7 8))

Outcome:

((List1) List2) 

What did i do wrong and how do i fix it?

Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
Dorirot9
  • 11
  • 3
  • That's not the code you wrote. If you want an explanation, post the code you actually have. Use copy & paste, don't retype. – molbdnilo Nov 13 '15 at 14:59
  • I suspect that you wrote `(cons '(List1) '(List2))`. Don't quote (`'`) things unless you mean to. (If you don't know what quoting means, you probably don't want to do it at all.) Go to the interactive window in DrRacket and play around with `cons` and various lists and other expressions to see what happens. – molbdnilo Nov 13 '15 at 15:11

1 Answers1

1

What you call characters of a list are generally called it's "elements". The first argument of cons will be treated as a single element of the resulting list, regardless if it's a number or a list, a list of lists etc.. In your example List1 is one such element.

If you are familiar with a little bit of math, think about sets. A set can contain e.g. numbers, but it can also contain other sets, which get also treated as a single element, regardless of their content. What you are searching for to merge the two lists is a function often called append. You should be able to define it yourself fairly easily recursively.

EDIT:

(define (intertwine a b)
  (cond [(empty? a)  b]
        [(empty? b)  a]
        [else
         (cons (car a) (cons (car b) (intertwine (cdr a) (cdr b))))]))

(define (main a b)
  (reverse (intertwine (reverse (intertwine a b))
                       b)))

When you call (main List1 List2) it will look like this in the intermediary steps:

(intertwine '(1 2 3 4 5 6 7 8) '(a b c)) = '(1 a 2 b 3 c 4 5 6 7 8)

(reverse '(1 a 2 b 3 c 4 5 6 7 8)) = '(8 7 6 5 4 c 3 b 2 a 1)

(intertwine '(8 7 6 5 4 c 3 b 2 a 1) '(a b c)) = '(8 a 7 b 6 c 5 4 c 3 b 2 a 1)

(reverse '(8 a 7 b 6 c 5 4 c 3 b 2 a 1)) = '(1 a 2 b 3 c 4 5 c 6 b 7 a 8)

AleArk
  • 417
  • 4
  • 16
  • This doesn't really get him closer to the solution. append takes two list arguments `lst1` `lst2` and makes a new list like *psuedocode*: `(lst1[0] lst1[1] ... lst1[n] lst2[0] lst2[1] ... lst2[n])` – Anandamide Nov 14 '15 at 03:33
  • What he needs is to zip the two lists together, but modifying the shorter list so that its length is `>` or `eq?` to the longer list. – Anandamide Nov 14 '15 at 03:34
  • While trying to find a solution he defined a helper function `match`, which is supossed to append. My answer was supossed to help just with that. – AleArk Nov 14 '15 at 09:35