简体   繁体   English

合并方案中的两个“列表中的列表”

[英]Merge two “list in list” in scheme

I don't know how to exactly express my question in language. 我不知道如何用语言准确表达我的问题。 Pardon my ambiguous words in the title 原谅我模棱两可的标题

My question is: 我的问题是:

If you have 如果你有

a = [[1, 2], [3, 4], [5, 6]....], b = [[1, 3], [4, 8], ....] a = [[1,2],[3,4],[5,6] ....],b = [[1,3],[4,8],....]

How to merge a and b like 如何像合并a和b

[[1,2], [3, 4], [5, 6], [1, 3], [4, 8]....]

in scheme?? 在计划中?

I have already tried using car or write a function my self to solve it, but all failed. 我已经尝试使用汽车或自己编写函数来解决它,但是都失败了。 I was thinking to use (cons ab), but it'll give me [[[1, 2], [3, 4]...], [[1, 3], [4, 8]...]] which is not what I want. 我本来打算使用(cons ab),但是它会给我[[[1,2],[3,4] ...],[[1,3],[4,8] ...] ],这不是我想要的。 I tried to write a recursive function, but still I'll get the similar trouble as (cons ab) 我试图编写一个递归函数,但仍然会遇到与(cons ab)类似的麻烦

Thanks! 谢谢!

As pointed out in the comments, you're looking for append . 正如评论中指出的那样,您正在寻找append This is how the code in the question would look like in Scheme: 这是问题中的代码在Scheme中的样子:

(define a '((1 2) (3 4) (5 6)))
(define b '((1 3) (4 8)))

(append a b)
=> '((1 2) (3 4) (5 6) (1 3) (4 8))

If you want to implement your own version of append it's simple enough: 如果要实现自己的append版本,它就很简单:

(define (my-append lst1 lst2)
  (if (null? lst1)
      lst2
      (cons (car lst1)
            (my-append (cdr lst1) lst2))))

(my-append a b)
=> '((1 2) (3 4) (5 6) (1 3) (4 8))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM