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)
Related
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)
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)))
The first function below searches a certain number provided when called. The goal is to reduce the the size of the search mechanism. Each time it looks up for the number it looks up for two ends only and reduces the look up half increasing efficiency.
def bsearch(s, e, first, last, calls):
print (first, last, calls)
if (last - first) < 2: return s[first] == e or s[last] == e
mid = first + (last - first)/2
if s[mid] == e: return True
if s[mid] > e: return bsearch(s, e, first, mid - 1, calls + 1)
return bsearch(s, e, mid + 1, last, calls + 1)
def search(s,e):
print bsearch(s, e, 0, len(s) - 1, 1)
when i run this for example like this:
s = range(1000000)
x = search(s, 5000000)
print x
It produces result like this:
(0, 999999, 1)
(500000, 999999, 2)
(500000, 749998, 3)
(500000, 624998, 4)
(500000, 562498, 5)
(500000, 531248, 6)
(500000, 515623, 7)
(500000, 507810, 8)
(500000, 503904, 9)
(500000, 501951, 10)
(500000, 500974, 11)
(500000, 500486, 12)
(500000, 500242, 13)
(500000, 500120, 14)
(500000, 500059, 15)
(500000, 500028, 16)
(500000, 500013, 17)
(500000, 500005, 18)
(500000, 500001, 19)
True
Notice how it reduces the look up mechanism. But i am stuck here:
if s[mid] > e: return bsearch(s, e, first, mid - 1, calls + 1)
return bsearch(s, e, mid + 1, last, calls + 1)
Can't understand what exactly it id doing here. Can anyone explain please
This is a classical example of recursion: a function calling itself, but with different arguments (first and last in this particular case). Note that the function assumes that the sequence searched is ordered: each subsequent member is not less than the previous one. This makes it possible to cut the searched space in half with every recursive call, because it is clear in which half the target e can occur.
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 ;)
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.