Saturday, November 10, 2012

School Rambles:

My favorite thing about school right now is that my programming class is going much better than my CS classes have been previously, I know this is largely due to renewed energy, but nevertheless, I like Racket.
Racket is a programming language that not many people know exists. It's a dialect of LISP and my only experience with LISP so far in my CS career. I enjoy this language because I think it has a lot of functionality that Java and C++ don't have [ha-ha, get it? Functionality? Becau-well-nevermind.], but my favorite thing of all is definitely car and cdr. Before this semester, I had taken a C++ class on Data Structures, and now, I'm taking another Data Structures class at a different school using Racket, and everything is made so much easier by car, cadr, cddr, cdr, etc.

So car, which stands for contents of address register, is the front of a construct [cons] and cdr is the rest of the construct. This means that car is one number, and cdr is a construct! Different types, guys-cool, no? [You can also say front and rest, but those are less fun for reasons I'll explain later].

A cons takes a pair, which many people might think is odd because a pair is not really a list, is it? But thanks to computers not being morons [WHOO~!] you can call cons inside of itself and make a list saying something like (cons 1 (cons 2 (cons 3 '() ))). That '() means an empty list, so it just stops there, and this would print out '(1 2 3), where car is 1 and cdr is '(2 3). Another [and possibly more accurate] way to think of this is defining the cons by the car and cdr--> (cons car cdr) or (cons 1 '(2 3)).

Now, because '(1 2 3) is a list, and cons takes a pair, you could make a new list without typing all those parenthesis by saying (cons 1 '(1 2 3)) and get '(1 1 2 3). As you can see, 1 is the car of the list and '(1 2 3) is the cdr of the list. If you want the second thing, you could type something like (first (rest list)) [The first of the rest of the list, assuming you called your construct list] which would be (car (cdr list)) [the car of the cdr of the list], but since car and cdr are fun, you can type (cadr list) [the cadr of the list] instead! You can keep going, too, like (caddr list) for the third thing. I don't know how to pronounce caddr. :(
The only problem is that I haven't been able to find the equivalent to init in other languages, and that would be very helpful for me right now. I'll post / update this if I ever figure it out.

So, I figured I'd post these sorts of things so I don't forget them, and hopefully my explanations of things, though specific, will be thorough enough to help others!
Keep up the nerdy, my friends :D! [I got to see Bill Nye the science guy last night(!) I'm definitely feeling the nerdy!]

No comments:

Post a Comment