List Manipulation
I am amused (because I am a weirdo) by how common the paradigm of manipulating lists is in computer programming, specifically, the need to separate the first element in a list from the rest of the list.
My very first exposure to dealing with lists was Logo, specifically, the first
and butfirst
primitives.
Of course, since Logo is technically a dialect of Lisp, these primitives are really just synonyms for car
and cdr
, which I remember from my college freshman roommate’s attempt to teach me Scheme. (I don’t know why, but I honestly could never get a hang of all those parentheses.)
My second exposure to lists (specifically, linked lists) was when I was studying for the A.P. Computer Science exam senior year in high school, even though I didn’t take the A.P. Computer Science class. Pascal was the language of choice at the time, and while I learned how to use pointers and how to insert and delete elements, I remember it requiring a lot of low-level programming and longed for the easy elegance of interpreted Logo.
So I feel like I’ve come circle dealing with lists in Ruby. Ruby doesn’t really have a list type, but since Arrays can be dynamically resized (unlike in Pascal or even many interpreted BASIC dialects), they can easily be used as such. Arrays have a #first
method but they don’t have a corresponding #butfirst
(Apparently that’s what Matz would call it if it existed. Yay Logo!)
But I guess it’s easy enough to use Array.drop(1)
(or even extend the Array class with a #butfirst
method that serves as an alias for Array.drop(1)
)
Oh, and there’s also the splat operator in Ruby:
The Strange Ruby Splat • 2011 Jan 21 • Adam Sanderson • End of Line