CSC302 2011S Programming Languages
[Skip to Body]
Admin:
[Front Door]
[Schedule]
[Handouts]
[Honesty]
[Piazzza]
Current:
[Current Outline]
[Current EBoard]
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Languages:
[Clojure]
[Erlang]
[Haskell]
[Io]
[Prolog (GNU)]
[Ruby]
[Scala]
Misc:
[SamR]
[CSC302 2007S]
[7L7W]
Summary: We begin our exploration of the Clojure programming language.
Prerequisites: Tate, Sections 7.1 and 7.2.
Contents:
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.)
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'})
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?
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?
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.
If you find yourself with extra time, further explore the differences between Clojure and Scheme.
Friday, 4 March 2011 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CSC302/2011S/Labs/clojure-1.html.
[Skip to Body]
Admin:
[Front Door]
[Schedule]
[Handouts]
[Honesty]
[Piazzza]
Current:
[Current Outline]
[Current EBoard]
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Languages:
[Clojure]
[Erlang]
[Haskell]
[Io]
[Prolog (GNU)]
[Ruby]
[Scala]
Misc:
[SamR]
[CSC302 2007S]
[7L7W]
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
;
;