Are there ruby equivalents to car, cdr, and cons?

前端 未结 5 938
终归单人心
终归单人心 2021-02-05 05:35

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]         


        
5条回答
  •  青春惊慌失措
    2021-02-05 06:34

    >> [1,2,3].drop 1
    => [2, 3]
    >> [1,2,3].first
    => 1
    

    Of course, as you know, these aren't too close to Lisp. The real ruby equivalent would be something like [1, [2, [3, nil]]]. You could always write a List class...or find one somewhere.

    Chapter 8 of Practical Ruby Projects is called Implementing Lisp in Ruby.

提交回复
热议问题