Re: Request for a hint

121 views
Skip to first unread message

Baishampayan Ghose

unread,
Nov 17, 2012, 12:04:40 PM11/17/12
to Clojure Group
Hi Milen,

The function `conj` "conjoins" an item into a Clojure collection. In your case, you need to define both `coll` and `item` for the example to work.

Consider these examples -

(conj [1 2 3] 4)
(conj '(1 2 3) 4)
(conj #{1 2 3} 4)

(def coll [1 2 3 4 5])
(def item 6)

(conj coll item)

etc.

I hope that helps.

-BG



On Sat, Nov 17, 2012 at 10:15 AM, Milen Ivanov <milen....@gmail.com> wrote:
Dear All,

I am trying to make sense of Closure by reading the book Programming Closure, 2nd Eddition.

I persistently get booed at one of the very first lines of code in the book:

(conj coll item)

The listing (rather short) is below.

May I please ask you for some hint because beginner's luck seems to have gone missing in this case.

Thanks in advance!
Milen


milen@linux-oaty:~> cd ~/Dropbox/Clojure/clojure-1.4.0/
milen@linux-oaty:~/Dropbox/Clojure/clojure-1.4.0> java -cp clojure-1.4.0.jar clojure.main
Clojure 1.4.0
user=> (conj coll item)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: coll in this context, compiling:(NO_SOURCE_PATH:1)
user=> (pst)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: coll in this context, compiling:(NO_SOURCE_PATH:1)
        clojure.lang.Compiler.analyze (Compiler.java:6281)
        clojure.lang.Compiler.analyze (Compiler.java:6223)
        clojure.lang.Compiler$InvokeExpr.parse (Compiler.java:3548)
        clojure.lang.Compiler.analyzeSeq (Compiler.java:6457)
        clojure.lang.Compiler.analyze (Compiler.java:6262)
        clojure.lang.Compiler.analyze (Compiler.java:6223)
        clojure.lang.Compiler$BodyExpr$Parser.parse (Compiler.java:5618)
        clojure.lang.Compiler$FnMethod.parse (Compiler.java:5054)
        clojure.lang.Compiler$FnExpr.parse (Compiler.java:3674)
        clojure.lang.Compiler.analyzeSeq (Compiler.java:6453)
        clojure.lang.Compiler.analyze (Compiler.java:6262)
        clojure.lang.Compiler.eval (Compiler.java:6508)
Caused by:
RuntimeException Unable to resolve symbol: coll in this context
        clojure.lang.Util.runtimeException (Util.java:170)
        clojure.lang.Compiler.resolveIn (Compiler.java:6766)
        clojure.lang.Compiler.resolve (Compiler.java:6710)
        clojure.lang.Compiler.analyzeSymbol (Compiler.java:6671)
        clojure.lang.Compiler.analyze (Compiler.java:6244)
        clojure.lang.Compiler.analyze (Compiler.java:6223)
nil
user=>
                                                                                   

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en



--
Baishampayan Ghose
b.ghose at gmail.com

Milen Ivanov

unread,
Nov 17, 2012, 5:32:26 PM11/17/12
to clo...@googlegroups.com
Dear Mr. Chose,

Thank you for your help!

So, the "bug" is then in the book. Fine.

Best Regards,
Milen

Charlie Griefer

unread,
Nov 17, 2012, 5:46:13 PM11/17/12
to clo...@googlegroups.com
On Sat, Nov 17, 2012 at 5:32 PM, Milen Ivanov <milen....@gmail.com> wrote:
> Dear Mr. Chose,
>
> Thank you for your help!
>
> So, the "bug" is then in the book. Fine.

There's no "bug" in the book.

You copied sample code that's meant to show the syntax of using conj
directly into the repo, using undeclared values.

(conj coll item) is not meant to run on its own. It's demonstrating
that the function conj takes a collection and an item, and returns a
new collection with the item added on.

So as BG pointed out, you can do:

(conj [1 2 3] 4), in which [1 2 3] is a collection (vector) and 4 is
the item to be conj'd, which returns a new vector [1 2 3 4].
(conj '(1 2 3) 4), in which '(1 2 3) is a collection (list) and 4 is
the item to be conj'd, which returns a new list (4 1 2 3)

If you want to do (conj coll item) you need to define coll and item,
which BG did as:

(def coll [1 2 3 4 5])
(def item 6)

Now that "coll" and "item" exist, you can do (conj coll item), which
is the same as doing (conj [1 2 3 4 5] 6).

But you cannot simply do (conj coll item), and this is not a "bug" in
the book, but rather a description of how conj works.

--
Charlie Griefer
http://charlie.griefer.com/

I have failed as much as I have succeeded. But I love my life. I love
my wife. And I wish you my kind of success.

Charlie Griefer

unread,
Nov 17, 2012, 6:11:37 PM11/17/12
to clo...@googlegroups.com
For the record, I point out that its not a "bug" specifically because
you will see this many many many many times throughout the book. In
fact, pretty much any time a new function or form is introduced.

(function-name argument-1 argument-2 ... argument-n)

This is not real code meant to be pasted into the REPL and evaluated,
but rather a high level example of how a particular function works.

Hope that helps :)

Russell Whitaker

unread,
Nov 17, 2012, 6:38:11 PM11/17/12
to clo...@googlegroups.com
On Sat, Nov 17, 2012 at 3:11 PM, Charlie Griefer <charlie...@gmail.com> wrote:
For the record, I point out that its not a "bug" specifically because
you will see this many many many many times throughout the book. In
fact, pretty much any time a new function or form is introduced.

(function-name argument-1 argument-2 ... argument-n)

This is not real code meant to be pasted into the REPL and evaluated,
but rather a high level example of how a particular function works.

Hope that helps :)


I wouldn't lay money on a bet that it does.

R
 
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Milen Ivanov

unread,
Nov 21, 2012, 6:03:49 AM11/21/12
to clo...@googlegroups.com
Dear Mr. Griefer,

Thank you!

I get it now after reading few more pages. Even the font is different than that of real code and, moreover, it is recommended that the reader gets the code from book's webpage. So, I keep on reading... First of all, I paid for the book, LOL, and, second, I need to learn some Clojure for better or worse. 

Best Regards,
Milen
Reply all
Reply to author
Forward
0 new messages