I am trying to do a comparison to find the largest number in a list. I know I could just use the (max 1 2 4 3) but I'm trying to write the function myself. However, I'm not too sure where the error is. Whenever I run this, the maximum is always 2. Am I doing a comparison wrong somewhere or forget something?
(defn maxVal [list]
(def maxValue 0)
(doseq [x list]
(println x maxValue)
(if > x maxValue)
(do (println x ">" maxValue)
(def maxValue x)))
(var-get #'maxValue))
(maxVal '(1 4 3 2))
The problem is that your code is evaluating > as a value, not invoking it as a function.
(if > x maxValue)
...evaluates to x, because the function > is neither nil nor false, and thus is truthy when evaluated as a boolean -- and x is in the "if true" position, just as maxValue is in the "else" position in this if. Consequently, the current indentation (while it reflects intent accurately) is a bit misleading; the do should be outside the if, since it takes place no matter what happened prior.
Presumably, what you instead want is:
(if (> x maxValue)
Related
The following function takes a range of Celcius temperatures, converts them to Fahrenheit, and puts them into an array.
(defn build-f [size]
(map float (vec (map #(+(/(* % 9)5)32) (range size)))))
My goal is then to print each converted temperature in the following format:
C[0] = 32 F
C[1] = 33.8 F
...
The format doesn't really matter. I'm new to functional programming and still thinking the OOP
way.
I tried to use the map function to do that. What should I go with? Map, reduce, for, doall?
So far I came up with the following:
display-format function should display something like this C[0] =. I'm not sure how to place that [index] there though.
(defn display-format [t-scale]
(println t-scale "=" ))
display-array function should use the map function to apply display-format
function to the array.
(defn display-array [t-scale array]
(map (display-format t-scale) array))
Am I going in the right direction?
Update:
Here the code I was going for. Hope it helps someone.
(defn display-table [from-scale array to-scale]
(map #(println (str from-scale "[" %1 "] = " %2 " " to-scale))
(range (count array)) array))
Please see this list of documentation. Especially study the Clojure CheatSheet.
For your purposes, study
doseq for looping over the values to print output
format for using %d and similar output formatting options
str for concatenating strings
I'd suggest making a function celcius->farenheit and then calling that in a loop. Call double on the input arg and it will avoid any worries about int vs float all the way downstream.
I always prefer mapv over map to eliminate problems from laziness
Enjoy!
I have written a recursive function in java and want to implement the same for Clojure.
The java code snippet is below.
private boolean solve(int i, int j) {
// some other code
if ((solve(i - 1, j)) == true) {
return true;
}
if ((solve(i, j + 1)) == true) {
return true;
}
// some code!!!!
return false;
}
How can this be implemented in Clojure?
an almost direct equivalent is
(defn solve [i j]
(or ;; some other code
(solve (dec i) j)
(solve i (inc j))
;; some code
))
Notice the absence of a return statement - everything is an expression.
In your java snippet "some other code" must have the ability to return truthy, otherwise you end up with a stack overflow (in place of "endless recursion").
The reason the code can be so short is that the or macro short-circuits evaluation (ie. it stops evaluation as soon as a meaningful result can be deduced).
In the clojure example "some other code" must be an expression that could evaluate to false or nil.
If you need to "return false immediately" from "some other code" your expression needs to be a bit more convoluted.
I am having hard time to understand the scope of the following code:
(define (create-counter (x 1))
(let ([count 0])
(lambda()
(let ([temp count])
(set! count (+ x count)) temp))))
if I use:
(let ((c (create-counter ))) (+ (c) (c) (c) (c)))
the code work however if i tried with:
(+ (create-counter)(create-counter)(create-counter)(create-counter))
This does not work and give me a 0. Can someone please help me to understand this thoroughly? if possible, please compare to other language like C/C++ it would be easier for me to catch the hold of this. Thanks
(define (create-counter (x 1))
(let ([count 0])
(lambda()
(let ([temp count])
(set! count (+ x count)) temp))))
Translates to:
auto create_counter(int x=1){
int count=0;
return [x,count]()mutable{
int r=count;
count+=x;
return r;
};
}
A simple C++14 function returning a closure object.
When you do this:
(let ((c (create-counter ))) (+ (c) (c) (c) (c)))
It is:
auto c = create_counter();
auto r = c()+c()+c()+c();
return r;
It creates one counter, then runs it 4 times, returning 0 1 2 3 and adding to 6.
In this case:
(+ ((create-counter))((create-counter))((create-counter))((create-counter)))
It is:
auto r = create_counter()()+create_counter()()+create_counter()()+create_counter()();
return r;
Which creates 4 counters, and runs each one once. The first time you run a counter you get 0. So this adds to 0.
The closure object has state. It returns a bigger number each time you call it.
Now you may not be familiar with C++11/14 lamnda.
auto create_counter(int x=1){
int count=0;
return [x,count]()mutable{
int r=count;
count+=x;
return r;
};
}
Is
struct counter {
int x,count;
int operator()(){
int r=count;
count+=x;
return r;
};
};
counter create_counter(int x=1){
return {x,0};
}
with some syntax sugar.
I fixed what seems to be a syntax error in your original code. I am no expert, so maybe I got it wrong.
As an aside, a briefer create counter looks like:
auto create_counter(int x=1){
return [=,count=0]()mutable{
int r=count;
count+=x;
return r;
};
}
When you call "create-counter", it creates a counter and then returns a procedure that refers to that particular counter. When you call "create-counter" four times, you're creating four separate counters; each procedure refers to its own counter. When you call "create-counter" once and then the resulting procedure four times, it's creating just one counter, and incrementing it four times.
It's a bit hard to compare this to C, since C and C++ are quite weak in the area of closures; it's not easy to return a function that's defined inside of another function.
The closest analog might be a "counter" object in C++; think of "create-counter" as the constructor for an object containing a single integer, and the resulting procedure as an "increment" method that increments the counter contained in that object. In your second example, then, you're creating four distinct objects, where in your first example, you're creating one object and calling its "increment" method four times.
I see the binding of recur is "parallel", however I don't get what that means.
I've tested the code below:
(defn parallelTest
"parallel binding test of recur "
[]
(loop [vectorA [1 2 3 4 5]
A (first vectorA)]
(if-not (= A nil)
(do (println vectorA) (println A)
(recur (rest vectorA) (first vectorA)))) ;Recur!
))
(parallelTest)
the output is
user=>
[1 2 3 4 5]
1
(2 3 4 5)
1
(3 4 5)
2
(4 5)
3
(5)
4
()
5
nil
so I assume the bindings are happened simultaneously instead of one by one?
Yes, in computer science, "in parallel" will generally mean simultaneously, as opposed to "sequentially" (in a specified order) or "concurrently" (in an arbitrary indeterminate order, which could be parallel or sequential with arbitrary sequence). Parallel binding is typically understood to mean that a result (left hand side) of one binding is not in the scope of the creation (right hand side) of another (as opposed to sequential binding, as seen in Clojure's let statement).
Sequential binding
a = 1
b = 2
Here
1 is evaluated
then bound to a
then 2 is evaluated
then bound to b
Parallel binding
a,b = 1,2
Here,
1 and 2 are evaluated, either in a determined order (such as left to right) or not, depending on the language specifications
the two results are bound to a and b, respectively.
If the expressions (here 1 and 2) are independant and side-effect free, it doesn't matter which binding you use, but in parallel you need to be aware of the exact evaluation order.
Now, in your case,
first (rest vectorA)
then (first vectorA) are evaluated (left to right)
then the results are bound to vectorA and A, respectively.
which is a parallel binding, as opposed to for example a let binding in Clojure which is sequential.
I'm trying to write a function that checks if a character is within a certain hexadecimal range.
I'm trying the code shown below:
(def current \s)
(and (>= current (char 0x20)) (<= current (char 0xD7FF)))
I get the following error:
java.lang.ClassCastException: java.lang.Character cannot be cast to
java.lang.Number (NO_SOURCE_FILE:0)
I assume because the >= operator expects a number, it tries to type cast it.In regular java, I could just do:
(current >= 0x20) && (current <= 0xD7FF)
explicitly convert it to an int first
(<= 0x20 (int current) 0xD7FF)
Chars aren't numbers in Clojure though it's easy to cast them into characters with the int function.
(number? \a) => false
(number? 42) => true
(number? (int \a)) => true
For casts to primitive types you can use the function with the name of the type you want (see (find-doc #"Coerce")).
(int (char 0x20))
(float ...)
(map double [1 2 3 4])
....