Racket/Scheme embed list within existing list

73 views Asked by At

I'm attempting to embed a list in to an existing list, the problem is explained below.

list1

bla
bla
bla
bla

list2

useful stuff
is here

while my function iterates list1 it picks a random point where it will then insert:

useful stuff
is here

the issue is that list2 now looks like:

bla
bla
useful stuff is here
bla
bla

instead of:

bla
bla
useful stuff 
is here
bla
bla

I'm totally confused, I've googled and read the documentation but I just can not find an answer, everything I have tried has failed.

1

There are 1 answers

1
soegaard On BEST ANSWER
#lang racket
(define list1 '("bla" "bla" "bla" "bla"))
(define list2 '("useful stuff" "is here"))

(define insertion-point (random (length list1)))

(append (take list1 insertion-point)
        list2
        (drop list1 insertion-point))

The result on the first run:

'("bla" "useful stuff" "is here" "bla" "bla" "bla")