返回列表中每个元素的第二个元素 [英] Return the second element for every element in a list

查看:250
本文介绍了返回列表中每个元素的第二个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有此列表'( (4 (1 2)) (5 (5 5)) (7 (3 1)) (1 (2 3)))

我正在尝试在Scheme中编写smth,以便获取列表中每个元素的第二个元素.因此结果看起来像'( (1 2) (5 5) (3 1) (2 3))

I am trying to write smth in Scheme in order to get the second element for every element in the list.. So the result will look like '( (1 2) (5 5) (3 1) (2 3))

到目前为止,我已经有了此代码.

I have this code so far..

(define (second  list1)
  (if (null? (cdr list1))
      (cdr (car list1))
      ((cdr (car list1))(second (cdr list1)))))

推荐答案

这是一个简单的解决方案:

Here's a straightforward solution:

(define (seconds lst)
  (map cadr lst))

通常,当您要转换列表的每个元素时,map是解决方法.

In general, when you want to transform every element of a list, map is the way to go.

这篇关于返回列表中每个元素的第二个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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