Good luck with the learning, by the way! I found that the language had a definite "click" after a few weeks, and then I was in for good. Because the syntax is simple, many of the examples you'll see as you work through the book have a similar shape to them; I think this makes it easier to start absorbing patterns in Clojure. Have fun :)

Linus Ericsson <mailto:oscarlinuserics...@gmail.com>
July 10, 2014 at 6:52 AM
You should try Clojure Programming (Halloway, Bedra). I felt enlightened after reading the first edition, the second edition is also very good!

http://pragprog.com/book/shcloj2/programming-clojure

/Linus

On Thursday, July 10, 2014, Stephen Feyrer <stephen.fey...@gmail.com <mailto:stephen.fey...@gmail.com>> wrote:
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com <mailto:clojure+unsubscr...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.
Stephen Feyrer <mailto:stephen.fey...@gmail.com>
July 10, 2014 at 2:37 AM
Hi Sam, Lee.

Thank you both.

It would appear that I am faced with the old adage, "A little knowledge is a dangerous thing".

Again thank you, you ' ve been a great help. If I may impose upon you a little further? Would either of you be able to recommend an introductory book either for Clojure or FP that might fit well with Clojure, for the mildly bewildered? I have a book Programming Clojure but it is written with an assumption of prior knowledge/experience which I don't have. By the way, Programming Clojure is a good book, I just don't think I fit within the target audience.



--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com <mailto:clojure+unsubscr...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.
Lee Spector <mailto:lspec...@hampshire.edu>
July 9, 2014 at 7:41 PM

Or -- I now see, instead of adding the "do" you could just remove the outermost parentheses after the parameter list. But as Sam and I said this is a bad way to go anyway -- you want to avoid the nested defs.

-Lee

Lee Spector <mailto:lspec...@hampshire.edu>
July 9, 2014 at 7:31 PM
On Jul 9, 2014, at 8:48 PM, Stephen Feyrer<stephen.fey...@gmail.com>  wrote:

Hi,

I tried to create the function below in a Lighttable instarepl.  In lieu of any 
better idea for formatting, the<   >  statements below indicate instarepl 
output.

(defn avged ([x]
((def sumed (reduce + x))<  10>
(def counted (count x))<  4>
(def result (/ sumed counted))<  5/2>
result
)))

(avged [1 2 3 4])<  java.lang.ClassCastException: java.lang.Long cannot be cast 
to clojure.lang.IFn
Var.java:392 clojure.lang.Var.fn
Var.java:423 clojure.lang.Var.invoke
(Unknown Source) user/avged>

The objective of this function is just a learning exercise.

I have three questions:

1. Why doesn't it work, return a value?

Hi Stephen,

The main reason it doesn't work is that the expression after the parameter list [x] is a 
list and will be interpreted as a function call, with the function being whatever is 
returned from the first thing in the list -- which in this case is the "def 
sumed" definition, which returns the defined var.

You could patch (not recommended!) this by adding "do" to the beginning of that 
list:

(defn avged
   ([x]
     (do
       (def sumed (reduce + x))
       (def counted (count x))
       (def result (/ sumed counted))
       result)))

That will work, but it's not idiomatic clojure -- it is almost never idiomatic 
clojure to have a def or defn within another defn. Also, you've used the syntax 
that allows for multiple arglist/body pairs, although you only need one so the 
extra layer of parentheses isn't necessary.

Here's how I might write it somewhat more idiomatically:

(defn avged
  [x]
  (let [sumed (reduce + x)
        counted (count x)
        result (/ sumed counted)]
    result))

=>  (avged [1 2 3 4])
5/2
=>  (float (avged [1 2 3 4]))
2.5


2. What does the error message mean?  and seeing this or similar again, how do 
I investigate to get a meaningful Clojure solution?

I think it means it's trying to call the value of sumed as a function. I'll 
leave it to others to suggest ways of understanding Clojure error messages (and 
just note for any of those others who have the ability to do something about 
this that it sure would be great to be able to get stack backtraces with values 
of locals regardless of IDE etc.).

3.  Code Style, what can I do to improve readability and form?

Myself, I'd probably do it without any variables at all, and name everything 
more meaningfully, e.g.:

(defn average-numbers
   "Returns the average of the provided numbers."
   [numbers]
   (/ (reduce + numbers)
      (count numbers)))

=>  (average-numbers [1 2 3 4])
5/2

  -Lee

Stephen Feyrer <mailto:stephen.fey...@gmail.com>
July 9, 2014 at 6:48 PM
Hi,

I tried to create the function below in a Lighttable instarepl. In lieu of any better idea for formatting, the < > statements below indicate instarepl output.

(defn avged ([x]
((def sumed (reduce + x)) < 10 >
(def counted (count x)) < 4 >
(def result (/ sumed counted)) < 5/2 >
result
)))

(avged [1 2 3 4]) < java.lang.ClassCastException: java.lang.Long cannot be cast to clojure.lang.IFn
Var.java:392 clojure.lang.Var.fn
Var.java:423 clojure.lang.Var.invoke
(Unknown Source) user/avged >

The objective of this function is just a learning exercise.

I have three questions:

1. Why doesn't it work, return a value?

2. What does the error message mean? and seeing this or similar again, how do I investigate to get a meaningful Clojure solution?

3.  Code Style, what can I do to improve readability and form?


Thanks.

--
Stephen Feyrer.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com <mailto:clojure+unsubscr...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.

--
Sam Ritchie (@sritchie)
Paddleguru Co-Founder
703.863.8561
www.paddleguru.com <http://www.paddleguru.com/>
Twitter <http://twitter.com/paddleguru>// Facebook <http://facebook.com/paddleguru>

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to