Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheme merge two lists into one

Tags:

scheme

how to design a function that merge two lists into one list. the first element of first list will be the first element of the new list and the first element of the second list will be the second element of the new list (a,b,c,d,e,f) (g,h,i) will be (a,g,b,h,c,i,d,e,f,)

like image 422
John Avatar asked Nov 30 '22 02:11

John


1 Answers

Here is a purely functional and recursive implementation in R6RS

(define (merge l1 l2)
      (if (null? l1) l2
          (if (null? l2) l1
              (cons (car l1) (cons (car l2) (merge (cdr l1) (cdr l2)))))))
like image 128
Aslan986 Avatar answered Dec 08 '22 01:12

Aslan986