
CS 296 — Getting Started in Clojure Version 0.6 Mattox Beckman Contents 1 About Clojure 2 1.1 Purpose . 2 1.2 History of Clojure ........................................ 2 1.3 Getting Clojure ......................................... 2 1.4 Typographic Conventions . 3 2 Numerics, Arithmetic, and Function Calls 3 2.1 Function Calls . 3 2.2 Mathematics . 4 2.3 Exercises . 4 3 Creating and Using Variables 4 3.1 Define . 4 3.2 Let.................................................. 4 3.3 Exercises . 4 4 Built-in Data Structures 4 4.1 Sequences . 4 4.2 Lists . 5 4.3 Vectors . 6 4.4 Hash Maps . 6 5 Creating Functions 8 5.1 Lambda . 8 5.2 Another Shortcut . 8 5.3 Defn . 9 6 Conditionals 9 6.1 What is truth? . 9 6.2 cond . 9 7 Records 10 8 Mutation 10 9 Looping 10 9.1 You don’t need to do it. 10 9.2 But I really want to use a loop! . 11 9.3 List Comprehensions . 11 10 Namespaces 12 11 Solutions to exercises 12 12 Colophon1 13 1 About Clojure In the mid 1990’s, Matthias Felleisen founded PLT, a group of researchers interested in Scheme. 1.1 Purpose They produced their own dialect of Scheme called, appropriately enough, PLT Scheme. In 2010, they The purpose of this document is to help a program- renamed PLT Scheme version 5.0 to Racket. mer get up to speed using Clojure. It is not meant Lisp was very influential for a long time, until the to be comprehensive, but to serve as a starting point dreaded AI Winter. Researchers began to doubt the so that other reference materials will make more ability of symbolic logic to mimic human intelligence, sense. and AI (Artificial Intelligence) became less popular. I assume that the reader has programmed before, Because Lisp was considered an “AI language,” it and thus will be familiar with variables, function also became less popular. calls, loops, and the like. I do not assume that the In spite of this, people continued to work with reader’s experience is extensive. Lisp, writing production code and creating new di- This document is organized by sections. Each sec- alects. We will ignore them all except for the one tion will introduce an aspect of Clojure. Included that concerns us now. In 2009, Rich Hickey released will be discussion, sample code, and some exercises. Clojure publicly. Because it could run on the There are many code examples. You should read JVM and have access to all of Java’s libraries, it this with a running Clojure environment so you generated a lot of excitement, and is quickly finding can type them in and play with them yourself. This its way into industry. is the fastest way to learn. When you are finished with these sections you 1.3 Getting Clojure should be ready to start programming! The very short version is “get Leinengen”. Leinen- 1.2 History of Clojure gen, or lein as the command is called, is a Clo- jure build tool. It can run Clojure programs, Clojure is a dialect of Lisp. Lisp is best under- but it can also manage Clojure packages very eas- stood as a family of languages. This language family ily. We will be using this extensively in the course. is ancient, one of the oldest languages still in use to- It can be found at http://leiningen.org. day. The fact that it is still in use is a testimony to You will definitely want a Clojure-aware editor. its expressive power and its ability to be modified to If you want to get started quickly, the IDE called the will of the programmer. Today there are many LightTable is very promising. It’s a simple .jar dialects of Lisp in use, some quite new. These in- file, and runs almost everywhere. It can use the vim clude Common Lisp, Scheme, Racket, Guile, keybindings, for those of you who know what those and of course, Clojure. are. This is one of our recommended environments. In 1958, John McCarthy created the Lisp pro- Visit http://www.lighttable.com to try it. gramming language at MIT. It was meant to be a The eclipse package has a plugin called theoretical exercise, but then one of his graduate stu- CounterClockwise that supports Clojure, and I dents converted it to assembly language, and thus am told that it is good. You can find it at the first Lisp interpreter came into being. http://code.google.com/p/counterclockwise. In 1962, the first Lisp compiler was written in There are two editors also worth mentioning. The Lisp. A language is usually considered a “toy” lan- learning curve on them is much higher, but the ben- guage until it can compile itself. efit is also greater once you are familiar with them. In the early 1970’s, Sussman and Steele developed The course web site has some resources for both of Scheme to study a programming concept called the these. actor.1 In 1975, the first paper on Scheme was The first is an ancient editor called vim. It is a published. modal editor, with keybindings optimized for touch- typists. You can edit very quickly with vim. You 1Actors are entities that have their own threads and states, and can communicate with other actors. They are an early will want to get some plugins to vim to work with attempt to understand distributed computing, and are still in Clojure: the details can be found on the course use today. web site. 2 The second is another ancient editor called emacs. (plus 10 20) ; => 30 The author of this guide started learning it 25 years ago (and it was already considered old then), though or on the following line, like this: still in constant development. It is likely the most common environment for Clojure programmers. (plus 10 20) For a best-of-all-worlds approach, someone has ;; => 30 even written a vim emulator in emacs. It is called evil, and the reader can decide if that is just a co- incidence or not. The spacemacs project has put a 2 Numerics, Arithmetic, and Function lot of effort into unifying the two editors. Calls 1.4 Typographic Conventions Clojure contains many numeric types. The usual The examples in this manual will be of two types: ones are there, such as integers and floats. They look files and interactive environments. A file listing will like you would expect. Note that the semicolon is look like this: the comment character in Clojure. (defn plus [a b] 10 ; Long Integers (+ a b)) 234.345 ; Floats Running the program from the interactive envi- It also has big integers. These allow as many digits ronment will look like this. as you have memory to store them. % lein repl 918741238974019237401239487 ; Bignums nREPL server started on port 43244 REPL-y 0.2.0 Clojure has fractions, though we propbably will Clojure~1.5.1 not use them much. You can convert them to floats Docs: (doc function-name-here) if you want. (find-doc "part-of-name-here") Source: (source function-name-here) 50/15 ; => 10/3 (float 50/15) ; => 3.3333333333333335 user=> (load-file "foo.clj") #'user/plus user=> (plus 10 20) 2.1 Function Calls 30 In Clojure, every computation begins with a parenthesis. This is the most noticeable feature of The % lein repl is the Unix prompt and com- the language, and perhaps the most important. In mand to start Clojure, and the user=> symbol is non-lisps, a function call might look like f(x,y). In the Clojure prompt. Your own prompts may look Clojure, it looks like different depending on how your environment is set up. f Most of the examples in this document will be run ( x y) from the Unix command line, but you could also use one of the editors we mentioned to take advantage So, to add two numbers 10 and 20, you would write of the graphic user interface. In order to reduce the amount of space it takes to (+ 10 20) demonstrate Clojure code, many documentation writers use the convention of writing an expression To add 3 × 3 to 4 × 4 you would write followed by a comment displaying its value. The two forms are inline, like this: (+ (* 3 3)(* 4 4)) 3 There are a few strengths to this setup. First, (def a 10) precedence is explicit. You all know the algebraic (def b (+ 15 5)) rules governing times and plus, but there are many (+ a b) ; => 30 other operators. Second, this allows functions to take a variable number of arguments. For example, Variables created by def persist throughout their you can say (+ 2 4 8) to get 14, and you can say (< scope. 10 x 20) to check if variable x is between 10 and 20. Here is some example code 3.2 Let (+ 10 20 30) ; => 60 The second way to create variables is with the let (- 50 20 1) ; => 29 form. The syntax is (let [v1 e1 ...] body). Un- (* 8 6 7 5 3 9) ; => 45360 like def, the variables created by let are temporary (mod 10 4) ; => 2 and local. They exist only in the body part of the let, and then disappear. You can define more than 2.2 Mathematics one variable at a time by adding more pairs. Clojure uses Java’s Math library to handle ma- This session illustrates the interaction between lo- chine math. There is a separate library if you want cal and global variables. to use such functions on extended precision numbers. We will not be needing that in this class.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages14 Page
-
File Size-