Notice that in the previous page, there was little mention of programming languages. That's because in the grand scheme of things, programming languages don't matter. They only matter because, for any given problem, one language might let us solve the problem in fewer lines of code over another, or one language might let us solve the problem more efficiently, and so on.
What of the problem of teaching computer science? Which language should we use for that? We have chosen Racket, a dialect of Lisp. We'll show basics of the language today, after which you can start thinking about computer science. As you learn more computer science, we'll incrementally show you more of the language.
Here's an example of an expression that demonstrates the three rules above:
(+ 1 2)
In this example, we're feeding the arguments
2 into the procedure
+, which adds numbers.
We should expect the answer to be 3.
Of course, a language is no good if no one speaks it. For programming languages, the dialog is usually between the programmer and a computer. An interpreter is a program that translates a particular language into actions and computations that the computer performs. Interpreters are one way to make computers do things, such as computing large prime numbers or counting all the distinct words used in all of Shakespeare's plays.
Let's start our Racket interpreter. We do this by opening a terminal and then typing in
racket and hitting Enter.
You've just started the Racket interpreter!
You can now type Racket expressions for the interpreter to evaluate:
-> (+ 1 2) 3
Type each example below into the interpreter to try it out. Before entering each example, take a moment and think about what the output should be. Some of these examples cause errors—why do they do that? (If something errors, the interpreter will output an error message.)
5 (+ 2 3) (+ 5 6 7 8) (+ (* 3 4) 5) (+) + (sqrt 16) (/ 3 2) (/ 3 0) 'hello (first 'hello) (first hello) (butfirst 'hello) (bf 'hello) (first (bf 'hello)) (first 274) (+ (first 23) (last 45)) (define pi 3.14159) pi 'pi (+ pi 7) '(good morning) '(+ 2 3)
In this section, we learned the basics of Racket. We also tried our hands at the Racket interpreter.