球拍/方案中的压缩功能 [英] zip function in Racket/Scheme

查看:144
本文介绍了球拍/方案中的压缩功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定两个列表,返回一个列表,其元素是大小为2的列表,这样对于 i - 列表,第一个元素是<$第一个原始列表的第一个元素,第二个元素是第二个原始列表的 i -th元素。如果一个列表比另一个小,那么结果列表的大小最小;如果其中一个列表为空,则返回一个空列表。例如:

 > (zip'(1 2)'(3 4))
'((1 3)(2 4))

> (zip'(1 2 3)'())
'()
> (zip'()'(4 5 6))
'()
> (zip'(8 9)'(3 2 1 4))
'((8 3)(9 2))
> (zip'(8 9 1 2)'(3 4))
'((8 3)(9 4))


解决方案

试试这样:

  (1 2 3)'(abc))

左右:

 (map list'(1 2 3)'(abc))





 (define zip(lambda(l1 l2)(map list l1 l2)))

- >(zip'(1 2 3)'(xyz))
'((1 x)(2 y)(3 z))


Given two lists, return a list whose elements are lists of size two, such that for the i-th list, the first element is the i-th element of the first original list, and the second element is the i-th element of the second original list. If one list is smaller than the other, the resulting list is of the smallest size; and so if one of the lists is empty, return an empty list. For example:

> (zip '(1 2) '(3 4))
'((1 3) (2 4))

> (zip '(1 2 3) '())
'()
> (zip '() '(4 5 6))
'()
> (zip '(8 9) '(3 2 1 4))
'((8 3) (9 2))
> (zip '(8 9 1 2) '(3 4))
'((8 3) (9 4))

解决方案

Try so:

(map cons '(1 2 3) '(a b c))

or so:

(map list '(1 2 3) '(a b c))


(define zip (lambda (l1 l2) (map list l1 l2)))

->(zip '(1 2 3) '(x y z))
'((1 x) (2 y) (3 z))

这篇关于球拍/方案中的压缩功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆