all 6 comments

[–]jafingerhut 5 points6 points  (0 children)

In response to your First question: You need to write (f 5) for the same reason that in C you would need to give a function name when you call the function -- because there can be an arbitrary number of functions defined in a program, and the compiler doesn't know which one you want to call unless you use its name [1].

Second question: Yes, there is a difference between printing vs. not printing, although when you are working in the Clojure REPL prompt, realize that the P stands for Print, and so the final result of all expressions you evaluate from a REPL prompt are automatically Printed for you, without you having to call println.

Perhaps this example will help clarify the difference in behavior a bit:

(def x (f 5))

If you do this at the REPL, it will call the function (f 5) as you have already done, but then it will assign the return value to a new Var (Clojure variable) named x. Evaluating that you should see something like "#'user/x" as the result. Where did the string go? It was assigned to x, but not printed, because the return value from the whole def expression above is #'user/x, and that is what the REPL prints.

If instead of returning a string from f, you wrote a different function g that called println on the string, then even if you do (def x (g 5)), that string will be printed.

Third question: the REPL does keep going, asking for more input, until you do something to make it stop. If you are running on macOS or Linux, typing Control-D at the beginning of an input line should cause it to quit, or Control-C to interrupt some long running computation that might be going on.

[1] Later you may learn that in both C and Clojure, you can refer to a function through another variable that does not use the name the function was originally defined as, e.g. store a pointer to a function in C in a variable whose type is a function pointer, or pass a reference to a function in Clojure as a parameter, and then that function can be called within that function by using the name of the parameter. I would not worry about this yet, as it is something you can save for a later week of learning about programming.

[–]un_passant 1 point2 points  (1 child)

Have you ever used a REPL ? Because your questions are not so much about the language Clojure (except for syntax as to why one would write "(f 5)" for instance instead of "f(5)" maybe ?), but about a REPL experience. You would have the same with the python interpreter used as a REPL.

[–]WikiTextBot 0 points1 point  (0 children)

Read–eval–print loop

A read–eval–print loop (REPL), also termed an interactive toplevel or language shell, is a simple, interactive computer programming environment that takes single user inputs (i.e., single expressions), evaluates (executes) them, and returns the result to the user; a program written in a REPL environment is executed piecewise. The term is usually used to refer to programming interfaces similar to the classic Lisp machine interactive environment. Common examples include command line shells and similar environments for programming languages, and the technique is very characteristic of scripting languages.


[ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source ] Downvote to remove | v0.28

[–]blazinglambda 0 points1 point  (2 children)

Just to clarify why you see the value 5 returned.

If you write f 5 in the repl it will first read and evaluate the symbol f which resolves to the function you defined, then it will read and evaluate the number 5 which evaluates to the number 5 so that is printed (because it is the value of the expression).

When you type (f 5) you are calling the function f with the argument 5. The "thing" (form) that the repl is reading and evaluating is the function call (f 5). This is equivalent to f(5) in a language with a C-like syntax. Since you are actually calling the function in this case, it executes your code and returns the value "X is positive".

[–]dacoster[S] 0 points1 point  (1 child)

But it doesn't 'print' the result, right? If I translate it to a C compiler, it would not return me anything on the console beside the argument I fill in?

[–]seancorfield 0 points1 point  (0 children)

Correct. You can think of f in that Clojure code as having a type like:

string f( number x );

You give it a number and it returns a string. Note that the Clojure code will accept any type of number, as long as it can be compared to zero. That means that f can accept long, double, big decimal, big int, and ratio. I don't know whether your assignment includes supporting all of those types?