n-Queens - Logic Error - determining a vertical threat - list

In this program I will be determining if placing a queen will cause a threat on the board. This will use positions 1 - 8 (could be expanded) as the rows and columns. If no piece is located in a column then the y for that row will be 0 (otherwise it is the corresponding y). An empty board would be as follows: ((1 0)(2 0)(3 0)(4 0)(5 0)(6 0)(7 0)(8 0))
I wrote the following program to determine if a given move will result in a vertical threat:
(defun THREAT? (x y)
(not (eq
(cdr (nth (car x) y ))
0
)
)
)
And this will be my input:
(THREAT? '(1 3) '(1 0)(2 4)(3 7)(4 3)(5 2)(6 8)(7 5)(8 1))
My idea with this is that I grab the car of the first argument (the x value that I am checking) use that to access the correct element in the second list and find the cdr of that element which will give me its y value. Then I check if it is 0 or not and if it is not 0, then it will be a threat. I have yet to add functionality for checking horizontal and diagonal so I suspect I could easily achieve this through either conditionals or if statements but I want to clear up issues in my understanding of this before moving on
My error is the following:
*** - EVAL: 2 is not a function name; try using a symbol instead
Any and all help is greatly appreciated!
After fixing that error I found that my program will always return true. when inputting '(1 3) '((1 0)...) it will return true despite the fact that (1 0)'s 0 should find that it is equal to 0, returning true and then negating the true thus returning false and yet my program will always return true.
Edit: Fixed previous error message

To see why (threat? '(1 3) '((1 0) (2 4) (3 7) (4 3) (5 2) (6 8) (7 5) (8 1))) returns true, you must look at the definition of nth
Note that the elements are numbered from zero, not one.
So, (nth 1 '((1 0) (2 4) (3 7) ...)) returns (2 4) (here we are again :-) and not (1 0).

The error message comes from the third argument (2 4), which is correct, because 2 is not a function name.
Lisp evaluates all arguments before passing it to a function. In the case of (2 4), it searches for a function named 2 and fails. The same would happen with the remaining unquoted arguments.
Your threat? function expects only two arguments, so I suppose, this should be something like
(threat? '(1 3) '((1 0) (2 4) (3 7) (4 3) (5 2) (6 8) (7 5) (8 1)))

Related

If statement with comparison

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)

How to place a negative number in a list

How can I assign a negative number to a list of numbers?
I read elsewhere that you can use (- number) to negate a number, but for some reason, this is not working as expected in a list: '((- 1) 2 3)).
(- 1) is an expression that evaluates to -1. Quoting (the ' in front of your code) prevents the evaluation of an expression. Therefore:
> '((- 1) 2 3)
'((- 1) 2 3)
but
> (list (- 1) 2 3)
'(-1 2 3)
or, if you read up about quasiquoting and you want to show off
> `(,(- 1) 2 3)
'(-1 2 3)

How to calculate this long recursion

Here is a code doing recursion which crashes for larger values:
int rec(int m,int n)
{
if(m==0)
return n+1;
if(m>0 && n==0)
return rec(m-1,1);
if(m>0 && n>0)
return rec(m-1,rec(m,n-1));
}
If I call the function rec(m,n):
with m=1 and n=2, the result I get is 4
with m=2 and n=2, it is 7,
with m=3 and n=2, it is 29
But it crashes for m=4 and m=2. Is there an alternate way to calculate it?
Ackermann's function can be stated non-recursively as
Ack( m, n ) = (2 opm (n + 3)) - 3
where opm is the m'th arithmetic operation in the order addition, multiplication, exponentiation, tower function, ...
In other words it explodes in value pretty darn instantaneously, with no way to represent those numbers as C++ integers.
For an explanation see my homepage from the 1990's :),
(http://web.archive.org/web/20120315031240/http://members.fortunecity.com/alf_steinbach/content/programming/narrow_topics/ackermann/ackermann.html)
rec(4,2)
-> rec(3, rec(4, 1))
->rec(3, rec(4, 0)
->rec(3, 1)
->rec(2, rec(3, 0))
->rec(2, 1)
->rec(1, rec(2, 0)) -->rec(1, 2) //return 4
->rec(1, 0) //return 2 -->rec(0, rec(1, 1)) //return 4
->rec(0, 1) //return 2 -->rec(0, rec(1, 0)) //return 3
-->return 2
this boils down to:
rec(4,2)
-> rec(3, rec(4, 1))
->rec(3, rec(4, 0)
->rec(3, 1)
->rec(2, rec(3, 0))
->rec(2, 1)
->rec(1, 4)
This can be solved further but the space is insufficient and will take a lot of time.
I am predicting that your application either crashes due to stackoverflow or due to reaching the limit of allowable recursions.
But I cannot say for sure...
#Cheers and hth. - Alf explains it mathematically in much more subtle manner ;)

What does parallel binding mean in Clojure

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.

How do I determine if a character is within a range in Clojure?

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