CSC302 2011S Programming Languages

Laboratory: Clojure (1)

Summary: We begin our exploration of the Clojure programming language.

Prerequisites: Tate, Sections 7.1 and 7.2.

Contents:

Preparation

a. Create a directory for the lab.

b. Open a browser window on Tate's examples, in case you want to try any of them.

c. Make sure that you know how to load files into Clojure. (Hint: Give them a .clj suffix and use (load "prefix"). If that's not enough, make sure that your CLASSPATH is set correctly.)

Exercises

Exercise 1: Examining Structures

What do you expect the results of each of the following to be? After predicting each, check your answer experimentally.

a. (first (list 'a 'b 'c))

b. (rest (list 'a 'b' 'c))

c. (first ['a 'b 'c])

d. (rest ['a 'b 'c])

e. (first #{'a 'b 'c})

f. (rest #{'a 'b 'c})

g. (first #{'a "Alpha" 'b "Beta"})

h. (rest (list 'a))

i. (rest ['a])

j. (rest #{'a})

k. (rest {'a "Alpha"})

l. (first nil)

m. (rest nil)

n. (last nil)

o. (cons 'a (list 'b 'c 'd))

p. (cons 'a ['b 'c 'd])

q. (cons 'a #{'b 'c 'd})

r. (cons 'a #{'b "Beta" 'c 'Charlie'})

Exercise 2: Fun with Quotes

a. What do you expect the result of the following to be?

(first (first '('(a b) '(c d))))

b. Check your answer experimentally.

c. What does this example suggest about why we discourage the use of quotes to create lists in 151?

Exercise 3: Fun with Scope

Consider the following code

(def x 5)
(defn f [y] (+ x y))

a. What do you expect (f 3) to be?

b. Check your anser experimentally.

c. Suppose we redefined x, as in

(def x 23)

What do you expect (f 3) to be?

d. Check your anser experimentally.

e. Suppose we locally defined x, as in

(let [x 100] (f 3))

What do you expect the result to be?

f. Check your anser experimentally.

g. What do you expect the result of the following code to be?

(defn g [a] (+ y a))
(def y 10)
(g 3)

h. Check your answer experimentally.

i. What do you expect the result of the following code to be?

(def y (list 1 2 3))
(defn g [a] (+ y a))
(g 3)
(def y 4)
(g 3)

j. Check your answer experimentally.

k. What have these exercises taught you about Clojure's scoping and type checking?

Exercise 4: Tate's Problems

These problems taken from the Tate Day 1 Self-Study.

a. Implement a function called (big st n) that returns true if a string st is longer that n characters.

b. Write a funcction called (collection-type col) that returns :list, :map, or :vector based on the type of collection col.

For Those with Extra Time

If you find yourself with extra time, further explore the differences between Clojure and Scheme.

 

History

Friday, 4 March 2011 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Mon Apr 25 08:06:40 2011.
The source to the document was last modified on Fri Mar 4 09:46:13 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC302/2011S/Labs/clojure-1.html.
A PDF version of this document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC302/2011S/Labs/clojure-1.pdf

You may wish to validate this document's HTML ; Valid CSS! ; Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu