Re: noob questions - Hello world + learning

2014-07-03 Thread Evan Zamir
New to Clojure (find it fascinating so far, in large part due to watching 
approximately a billion Rich Hickey vids on YouTube). I had a similar 
question and figured I just resurrect this thread on it.

My naive thought was that when you (re)def a variable, you aren't actually 
copying over the old memory location, but instead simply re-creating a 
pointer to a new value or place in memory. To take this example further, 
I'm wondering what goes on under the hood when I use replace like this:

(def my-vec [1 2 3 4 5])
= (var user/my-vec)
(def my-vec (assoc my-vec 2 hello))
= (var user/my-vec)
my-vec
= [1 2 hello 4 5]

Is there a good explanation out there of what happens to the old my-vec and 
whether the new my-vec actually overwrites it, points to it, or points to a 
completely new location in memory?

Another way to phrase this question: is this real mutability or just the 
appearance of mutability?


On Tuesday, June 15, 2010 4:51:00 AM UTC-7, Joost wrote:

 On Jun 14, 10:09 pm, Jared tri...@gmail.com wrote: 
  Also, I thought this language is functional but I'm able to do change 
  declarations in the repl. For example: 
  user= (def x 1) 
  #'user/x 
  user= x 
  1 
  user= (def x 2) 
  #'user/x 
  user= x 
  2 
  user= (def x (+ 1 x)) 
  #'user/x 
  user= x 
  3 
  
  Why does that happen? This seems to go against the ideas of functional 
  programming. I can do the same things with functions too. 

 It's true that def isn't a strictly functional construct. 

 That's by design, since def is the construct that allows you to define 
 and set globally accessible vars, which includes all globally 
 accessible functions. 

 If you couldn't re-def a var, you couldn't redefine functions while 
 coding (or modify/extent existing functions using macros etc). Clojure 
 (like most Lisps) is a dynamic language with strong support for 
 interactive development - and you need redefineable vars (or something 
 similar) for that. The alternative is to recompile/load the whole 
 program every time you change a function definition. And that sucks 
 too much. 

 In idiomatic code, you only use (def) and its variants to set up 
 globally reachable data/functions across all threads and you don't, 
 for example, use def as a way to get variable variables - you will 
 get bitten if you try. 


-- 
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.


Re: noob questions - Hello world + learning

2014-07-03 Thread Andy Fingerhut
In your example, [1 2 3 4 5] allocates and initializes a vector with the 5
elements 1 2 3 4 5.

The first (def my-vec ...) also allocates a Var, and makes it 'point' at
the vector [1 2 3 4 5].

When you do (assoc my-vec 2 hello), it looks up the current value pointed
at by my-vec, which is the immutable vector [1 2 3 4 5], and it creates a
new vector [1 2 hello 4 5].  That new vector is also immutable.  In the
common case, it does not copy the entire vector [1 2 3 4 5] and then modify
the element 3 to hello -- it uses a technique called 'path copying' to
leave most of the memory that has been allocated shared between the two
vectors [1 2 3 4 5] and [1 2 hello 4 5].

The second (def my-vec ...) does mutate the 'pointer' for my-vec so that it
no longer points at [1 2 3 4 5], but instead points at [1 2 hello 4 5].
This is true mutation, not of the vectors, but of the Var my-vec.  Such
mutation helps support redefinition of functions during interactive REPL
sessions, among other things.

Andy


On Thu, Jul 3, 2014 at 2:39 PM, Evan Zamir zamir.e...@gmail.com wrote:

 New to Clojure (find it fascinating so far, in large part due to watching
 approximately a billion Rich Hickey vids on YouTube). I had a similar
 question and figured I just resurrect this thread on it.

 My naive thought was that when you (re)def a variable, you aren't actually
 copying over the old memory location, but instead simply re-creating a
 pointer to a new value or place in memory. To take this example further,
 I'm wondering what goes on under the hood when I use replace like this:

 (def my-vec [1 2 3 4 5])
 = (var user/my-vec)
 (def my-vec (assoc my-vec 2 hello))
 = (var user/my-vec)
 my-vec
 = [1 2 hello 4 5]

 Is there a good explanation out there of what happens to the old my-vec
 and whether the new my-vec actually overwrites it, points to it, or points
 to a completely new location in memory?

 Another way to phrase this question: is this real mutability or just the
 appearance of mutability?


 On Tuesday, June 15, 2010 4:51:00 AM UTC-7, Joost wrote:

 On Jun 14, 10:09 pm, Jared tri...@gmail.com wrote:
  Also, I thought this language is functional but I'm able to do change
  declarations in the repl. For example:
  user= (def x 1)
  #'user/x
  user= x
  1
  user= (def x 2)
  #'user/x
  user= x
  2
  user= (def x (+ 1 x))
  #'user/x
  user= x
  3
 
  Why does that happen? This seems to go against the ideas of functional
  programming. I can do the same things with functions too.

 It's true that def isn't a strictly functional construct.

 That's by design, since def is the construct that allows you to define
 and set globally accessible vars, which includes all globally
 accessible functions.

 If you couldn't re-def a var, you couldn't redefine functions while
 coding (or modify/extent existing functions using macros etc). Clojure
 (like most Lisps) is a dynamic language with strong support for
 interactive development - and you need redefineable vars (or something
 similar) for that. The alternative is to recompile/load the whole
 program every time you change a function definition. And that sucks
 too much.

 In idiomatic code, you only use (def) and its variants to set up
 globally reachable data/functions across all threads and you don't,
 for example, use def as a way to get variable variables - you will
 get bitten if you try.

  --
 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.


-- 
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.


Re: noob questions - Hello world + learning

2010-06-15 Thread Jared
I want to thank everyone for the helpful comments, and no worries
about the typing nickikt. I'll check out SICP. I've been playing
around in the repl, getting used to prefix notation, and I found out
the doc command. It makes learning this stuff much faster.

 I guess I should have been more clear about my level of CS/
programming knowledge. I took the CS-AP AB exam about 4 years ago, so
I know a little, and am slowly remembering a little more. The most
complex program I've written was a python program that set up the
initial game state for the board game Settlers of Catan. So I have a
small amount of familiarity with data structures, big O notation and
writing a few scripts.

I'm trying to grok this hello world template before I move on to other
stuff. What is the ns line for? I read the documentation on ns, but it
didn't make much sense to me. Is ns related to scope? All I know is
when I delete the ns line the program doesn't compile.

Why is it that function declarations must have the arguments as
vector(s), and not lists? As in, why can't I declare a function like
this?
(defn my-test
  (list n)
 (+ n 1))

In that default template, why does that hello world program even run?
At what line is main called? By the way, thank you nickikt for the
explanation that main is an overloaded function. That was not obvious
to me.

Also, I thought this language is functional but I'm able to do change
declarations in the repl. For example:
user= (def x 1)
#'user/x
user= x
1
user= (def x 2)
#'user/x
user= x
2
user= (def x (+ 1 x))
#'user/x
user= x
3

Why does that happen? This seems to go against the ideas of functional
programming. I can do the same things with functions too.

Also, is it preferred to put all this miscellaneous questions I come
up with in this thread, or to make a new topic for each question?

-- 
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


Re: noob questions - Hello world + learning

2010-06-15 Thread Joost
On Jun 14, 10:09 pm, Jared tri...@gmail.com wrote:
 Also, I thought this language is functional but I'm able to do change
 declarations in the repl. For example:
 user= (def x 1)
 #'user/x
 user= x
 1
 user= (def x 2)
 #'user/x
 user= x
 2
 user= (def x (+ 1 x))
 #'user/x
 user= x
 3

 Why does that happen? This seems to go against the ideas of functional
 programming. I can do the same things with functions too.

It's true that def isn't a strictly functional construct.

That's by design, since def is the construct that allows you to define
and set globally accessible vars, which includes all globally
accessible functions.

If you couldn't re-def a var, you couldn't redefine functions while
coding (or modify/extent existing functions using macros etc). Clojure
(like most Lisps) is a dynamic language with strong support for
interactive development - and you need redefineable vars (or something
similar) for that. The alternative is to recompile/load the whole
program every time you change a function definition. And that sucks
too much.

In idiomatic code, you only use (def) and its variants to set up
globally reachable data/functions across all threads and you don't,
for example, use def as a way to get variable variables - you will
get bitten if you try.

-- 
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


Re: noob questions - Hello world + learning

2010-06-15 Thread David Nolen
On Mon, Jun 14, 2010 at 4:09 PM, Jared tri...@gmail.com wrote:

 I'm trying to grok this hello world template before I move on to other
 stuff. What is the ns line for? I read the documentation on ns, but it
 didn't make much sense to me. Is ns related to scope? All I know is
 when I delete the ns line the program doesn't compile.


It's might not be that constructive to grok the hello world template before
moving on. That template is largely about creating a Java executable.

ns creates a namespace. It allows your program to modular and prevents the
likelihood of name clashes.

my-namespace/foo
your-namespace/foo

This way to two functions can have the same name yet be used together in the
same program.


 Why is it that function declarations must have the arguments as
 vector(s), and not lists? As in, why can't I declare a function like
 this?
 (defn my-test
  (list n)
 (+ n 1))


This is a Clojure syntax thing. Bindings happen inside of vectors.


 In that default template, why does that hello world program even run?
 At what line is main called? By the way, thank you nickikt for the
 explanation that main is an overloaded function. That was not obvious
 to me.


ns declaration here is being used to generate a Java class from this
namespace. The class that defines main in a Java program is the entry point.


 Also, I thought this language is functional but I'm able to do change
 declarations in the repl. For example:
 user= (def x 1)
 #'user/x
 user= x
 1
 user= (def x 2)
 #'user/x
 user= x
 2
 user= (def x (+ 1 x))
 #'user/x
 user= x
 3


This is definitely one way to introduce mutability into your program but
this is considered bad form.

David

-- 
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

Re: noob questions - Hello world + learning

2010-06-15 Thread Adrian Cuthbertson
Hi Jared,

Some good clojure specific learning resources;

http://java.ociweb.com/mark/clojure/article.html

and the essential http://clojure.org/

- Regards, Adrian

-- 
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


Re: noob questions - Hello world + learning

2010-06-14 Thread nickikt
Hello,

I have a some comments (on comments and more :))

First this
(comment . )

There are diffrent kind ofs comments. If you just want to note
something you can do ; but mabey you want to have a example how to use
something in your Code then use (comment ).

With defn you creat a new function with a name.

First switch the order of your function implementation

(defn -main
 ([] (-main world))
 ([greetee]
(println (str Hello  greetee !

What you are doing is implementing the -main method twise one for no
arguments once for one argument ([] and [greetee])

This is a handy because you can make an easy and fast default
implementation. An other way would be doing it like this

(defn -main
 ([] (println Hello world))
 ([greetee]
(println (str Hello  greetee !

But if you want to change something (Hello to helo or something) you
would have to do this two times. So you just call your second
implementation ([greetee] .. ) with world. So a change got
something in ([greetee] ) works for both implementations.

So what does (str Hello  greetee) do?

Lets say you call -main like this (-main nickik) then the greetee
variable is a string. With (str ...) you make one string out of the
arguments.

(str Hallo  nickik) -- Hallo nickik

than that is printed.

If you call -main like this (-main 75) is will work because 75 will be
converted to 75.

Because you new some additional information. In most langauges you
would have to tell the compiler what greetee is ( int or string )
these are what we call static languages but in clojure you dont have
to. That is wath makes a language dynamic. So you can pass in
everything that can be converted to a string.

Interessting that you learn programming with clojure. I had to unlearn
so many bad stuff that I was used to.

If you want to use a lot of clojure you should learn git a liddle bit
(there is a free book Pro Git) most of clojure stuff people do is on
github.org

You can try the laprepl http://github.com/relevance/labrepl its made
to help getting started

Some links for you to finish this up some are probebly hard for you:
http://clojure.blip.tv/  -- videos about clojure (most of them from
rich himself if  you are hot for more surf infoq.com)

The book that  David Nolen mentiond:
http://mitpress.mit.edu/sicp/full-text/book/book.html and Videos to
help you learn you can found here:
From MIT 1986: http://www.youtube.com/watch?v=2Op3QLzMgSY  and the
next 20 vids but you can download them too just google them.

Or if you like  Berkeley more http://www.youtube.com/watch?v=zmYqShvVDh4

More on Programming in (its in java but it starts at lvl absolute
ZERO)
http://www.youtube.com/user/stanforduniversity?blend=2ob=4#g/c/84A56BC7F4A1F852

But i recommend stike with the SICP book and courses they are harder
but its worth it. Specaly if you like clojure.

Sorry for tipping errors

Great

On Jun 11, 5:47 pm, Jared tri...@gmail.com wrote:
 Hi everyone,

 I'm 100% new to LISP, 95% new to Java, and 90% new to programming in
 general. Where and how would you recommend learning Clojure? I'm
 planning on buying Programming Clojure, but until then what would you
 suggest?

 I got Netbeans working with Clojure and the default template is this:

 (comment
 Sample clojure source file
 )
 (ns com.yourcompany.defpackage
     (:gen-class))

 (defn -main
     ([greetee]
   (println (str Hello  greetee !)))
   ([] (-main world)))

 It ran and printed Hello world!. Can anyone explain to me in detail
 what each statement means. I'm particularly confused by:

 (comment
 Sample clojure source file
 )

 I thought all comments began with ; ? I understand what println does
 and that's it.

 Thanks in advance everyone.

-- 
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


Re: noob questions - Hello world + learning

2010-06-13 Thread David Nolen
If your are new to programming I recommend reading at least the first
three chapters of The Structure and Interpretation of Computer
Programs. It's available online.

David

On Friday, June 11, 2010, Jared tri...@gmail.com wrote:
 Hi everyone,

 I'm 100% new to LISP, 95% new to Java, and 90% new to programming in
 general. Where and how would you recommend learning Clojure? I'm
 planning on buying Programming Clojure, but until then what would you
 suggest?

 I got Netbeans working with Clojure and the default template is this:

 (comment
 Sample clojure source file
 )
 (ns com.yourcompany.defpackage
     (:gen-class))

 (defn -main
     ([greetee]
   (println (str Hello  greetee !)))
   ([] (-main world)))

 It ran and printed Hello world!. Can anyone explain to me in detail
 what each statement means. I'm particularly confused by:

 (comment
 Sample clojure source file
 )

 I thought all comments began with ; ? I understand what println does
 and that's it.

 Thanks in advance everyone.

 --
 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 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


Re: noob questions - Hello world + learning

2010-06-13 Thread Moritz Ulrich
I don't think that Structure and Interpretion of Computer Programs is
a good first-book if you want to start lisp-programming (Especially
Clojure). It's very detailed and gives much insights, but Clojure is
way more practical than this book. If you want to start programming
Clojure, I'd recommend Pragmatic Programming Clojure or The Joy of
Clojure (Even when JoC isn't finished yet).

To your original question: That's a hard piece of code to start with.
I'd recommend firing up a repl and trying with this instead of this
template you've mentioned.

On Sun, Jun 13, 2010 at 6:25 PM, David Nolen dnolen.li...@gmail.com wrote:
 If your are new to programming I recommend reading at least the first
 three chapters of The Structure and Interpretation of Computer
 Programs. It's available online.

 David

 On Friday, June 11, 2010, Jared tri...@gmail.com wrote:
 Hi everyone,

 I'm 100% new to LISP, 95% new to Java, and 90% new to programming in
 general. Where and how would you recommend learning Clojure? I'm
 planning on buying Programming Clojure, but until then what would you
 suggest?

 I got Netbeans working with Clojure and the default template is this:

 (comment
 Sample clojure source file
 )
 (ns com.yourcompany.defpackage
     (:gen-class))

 (defn -main
     ([greetee]
   (println (str Hello  greetee !)))
   ([] (-main world)))

 It ran and printed Hello world!. Can anyone explain to me in detail
 what each statement means. I'm particularly confused by:

 (comment
 Sample clojure source file
 )

 I thought all comments began with ; ? I understand what println does
 and that's it.

 Thanks in advance everyone.

 --
 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 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



-- 
Moritz Ulrich
Programmer, Student, Almost normal Guy

http://www.google.com/profiles/ulrich.moritz

-- 
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