Any idea of how to interleave two lists in dr racket?

淺唱寂寞╮ 提交于 2019-12-13 14:07:49

问题


The problem is when lists have a different length, any idea of how to do it?

I have to use functions like map or something like that

This is the code I wrote so far, it works with lists of the same length but it also needs to work with lists of different lengths. Thank you.

(define (interleave list1 list2)
 (flatten [map (lambda (x y) (cons x (cons y null))) list1 list2]))

if lists have different length this is what I get:

map: all lists must have same size; arguments were: # '(1 2 3 4 5) '(a b c)

I'm trying to get (1 a 2 b 3 c 4 5)


回答1:


#lang racket

(define (weave xs ys)
  (match (list xs ys)
    [(list (cons x xs) (cons y ys)) (cons x (cons y (weave xs ys)))]
    [(list '() ys)                  ys]
    [(list xs '())                  xs]))



回答2:


I'm assuming your desired behavior is that the lists are interleaved for as long as this is possible, and then whatever is left over from the nonempty list is appended to the end. In that case one possible implementation is

(define (interleave a b)
  (if (null? a)
      b
      (cons (car a)
            (interleave b (cdr a)))))

I think this is probably the simplest possible way to write what you're looking for.




回答3:


Neither map nor fold-right would work because they either signal an error when one list is smaller than the other or they tend to stop at the shortest list. eg. SRFI-1's map (interleave '(1 2 3 4) (circular-list 9 8)) ; ==> (1 9 2 8 3 9 4 8). For a different behavior you need to roll your own.




回答4:


A solution using simple list manipulation functions might be:

 (define (interleave list1 list2)
   (cond ((empty? list1) list2)
         ((empty? list2) list1)
         (else 
          (append 
           (list (car list1) (car list2))
           (interleave (cdr list1) (cdr list2))))))

Testing...

 > (interleave '(1 2 3 4 5) '(a b c))
 (1 a 2 b 3 c 4 5)
 > (interleave '(1 2 3 4 5) '())
 (1 2 3 4 5)
 > (interleave '() '(a b c))
 (a b c)
 > 

I think it is fairly self-documenting.




回答5:


"There ain't nothin' you can't not do with fold-right and some of them con-tin-uations thingies", said a cowboy to another, spittin' into the campfire and puffin' on his cigar in the evening, sippin' his black coffee from his rugged banged up tin mug. "Yessa, nothin' in the whole darn world."

(define (interleave xs ys)

    ;; interleave xs ys = foldr g n xs ys
    ;;    where
    ;;    g x r (y:ys) = x : y : r ys
    ;;    g x r []     = x : r []
    ;;    n ys         = ys

    ((foldr
       (lambda (x r)
         (lambda (ys) 
           (cond ((null? ys) (cons x (r '())))
                 (else (apply (lambda (y . ys)
                                 (cons x (cons y (r ys))))
                              ys)))))
       (lambda (ys) ys)
       xs)
     ys))


来源:https://stackoverflow.com/questions/30071930/any-idea-of-how-to-interleave-two-lists-in-dr-racket

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!