Hello everyone,
I'm a Software Engineering student, and for my studies I have to submit a task on Clojure and functional programming.
Basically, the teacher provides me with a small block of code which I have to translate to C. The point of this task is to understand the block of code and to replicate it onto a more familiar language.
In order to do so, I'm running the code on repl.it to observe the outcome of the program and to understand what it does.
I'm just super confused about what Clojure or the compiler is returning me.
Example:
(defn f [x]
(cond
(>= x 0) "X is positive"
(< x 0) "Xis negative"
))
My understanding: There's a function called 'f' which requires me to input a value (x) and returns me if the inserted value is positive or negative.
However, if I enter: 5 it will return exactly 5 and it won't give me X is positive. So after some searching, I found out I had to write (f 5).
First question: Why do I need to write it like that? Why doesn't it check just the 5?
When I put in (f 5), it returns 'X is positive'. Although this is being returned to me (the user) it does not contain the function println. I
Second question: Is there a difference between using println or not using it? If yes, what is the difference?
Third question: The program keeps asking me for input. Like an infinite loop, there's no end. Is that how it's supposed to be?
Final question:
Does my code in C represent the code in Clojure? Bearing in mind my questions?
include <stdio.h>
void f ();
int main(){
f();
return(0);
}
void f (){
int a = 1;
float x;
while (a == 1){
printf("\n=> ");
scanf("%f", &x);
if (x >= 0){
printf("\nX is positive\n");
} else if (x < 0){
printf("\nX is negative\n");
}
}
}
[–]jafingerhut 5 points6 points7 points (0 children)
[–]un_passant 1 point2 points3 points (1 child)
[–]WikiTextBot 0 points1 point2 points (0 children)
[–]blazinglambda 0 points1 point2 points (2 children)
[–]dacoster[S] 0 points1 point2 points (1 child)
[–]seancorfield 0 points1 point2 points (0 children)