是否有与car,cdr和cons等价的红宝石? [英] Are there ruby equivalents to car, cdr, and cons?

查看:73
本文介绍了是否有与car,cdr和cons等价的红宝石?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有与lisp car,cdr和cons功能相当的红宝石功能?对于那些不熟悉Lisp的人,这是我想要的红宝石:

Are there ruby equivalents to the lisp car, cdr, and cons functions? For those unfamiliar with lisp, here's what I want from ruby:

[1,2,3].car   => 1
[1,2,3].cdr   => [2,3]
[2,3].cons(1) => [1,2,3]

(以Lisp表示):

(car '(1 2 3))  => 1
(cdr '(1 2 3))  => (2 3)
(cons 1 '(2 3)) => (1 2 3)

推荐答案

Ruby数组未实现为单链接列表,因此,拥有car,cdr和东西并不是那么有用.

Ruby arrays are not implemented as singly-linked lists, so it is not as useful to have car and cdr and stuff.

如果您真的想要,可以做

If you really wanted, you could do

[1,2,3][0]      => 1
[1,2,3].first   => 1
[1,2,3][1..-1]  => [2,3]
[1] + [2,3]     => [1,2,3]

这篇关于是否有与car,cdr和cons等价的红宝石?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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