Questions about using recursive functions in lisp - lisp

I'm a beginner at lisp. I'm grateful that stackoverflow helped me a lot.
First, I want to express the recursive function using the lisp function.
There is no compilation error, but I want to check if the code I wrote is grammatically correct.
Below is the C code I wrote.
void queens(int i)
{
int j;
if (promising(i))
{
if (i == n)
{
//print all col list
return
}
else
{
for (j = 1; j <= n; j++)
{
col[i + 1] = j;
queens(i + 1);
}
}
}
}
Below is the lisp code.
(defvar n 4)
(defvar col (list 0 0 0 0 0 ))
(defun queens(i)
(let ((j 1))
(if (promising i)
(if (= i n) (print col)
(loop while (<= j n) do (setf (nth (+ 1 i ) col) j)
( queens (+ 1 i ) )
(incf j)))
)
))

First, you (still) need to fix the indentation, alignment, and general style. This is not merely to annoy you, but because it actually makes things clearer, and easier to debug. For example, if you do not indent properly, it becomes hard to see where each expression ends, and this is even worse when using complex macros such as loop. Your code, properly formatted but without any other modification, should be:
(defun queens (i)
(let ((j 1))
(if (promising i)
(if (= i n)
(print col)
(loop while (<= j n)
do (setf (nth (+ 1 i) col) j)
(queens (+ 1 i))
(incf j))))))
The closing parenthesis is placed right after the last form of the expression, without line break, without whitespace.
The opening parenthesis is also right before the first element of the form, without whitespace, but should be detached from what comes before, unless it is another parenthesis (so (defun queen (i) ...) is correct, while ( defun queens(i)) breaks two of the rules).
When using an if construct, either you write it all on a single line (the condition, the "then" form and the "else" form), or you have to insert line breaks after each of them. Otherwise, it is unclear when the "then" form ends and when the "else" starts.
When defining special variables with defvar, name them with '*' around their names, as in (defvar *n* 4) and (defvar *col* (list ...)).
Another style considerations, which helps with problems related to nesting: use when and unless instead of if if you are only interested in one of the two branches. This makes intent clear, and can also simplify the syntax in case of complex branches.
Now, for the code:
If you want to use loop, use the basic for constructs. Instead of writing
(loop while (< j n) do ... (incf j))
use
(loop for j from <start> to <end> do ...)
You can also use upto, below ... if you want subtly different termination conditions.
When you do this, you do not need to introduce a binding for j using let, it will be introduced by the loop.
If you want to modify a list, use a vector instead. Instead of defining col as (list ...), simply use (vector ...) or the make-array function. Afterwards, use elt or aref to access individual elements instead of nth which only works for lists. A version of the code which assumes that all the previous remarks have been taken into account:
(defun queens (i)
(when (promising i)
(if (= i n)
(print *col*)
(loop for j from 1 to *n*
do (setf (aref *col* (1+ i)) j)
(queens (1+ i))))))
Without knowing what promising does and what queens is supposed to do, it is hard to give more insight, but the algorithm is weird nonetheless. For example, in a given call to (queens i), the lexically apparent setf will only ever modify the i+1-th cell of col, setting it to a new j at each iteration. As far as I can tell, this simply results in (aref col (1+ i)) being set to n at the end of the loop. It also seems that you do not use the value recursively returned by queens (which is to be expected: your C function returns nothing !), and that you do not really do any check involving col, so a recursive solution seems weird, there is (seemingly) no relation between (queens i) and the (queens (1+ i)) that it calls n times in a loop !
If the above paragraph is irrelevant because promising also heavily modifies col: stop doing C-in-Lisp and stop modifying things all around the place. Don't rely on state, mutation, global variables & the like, when you can avoid it. It is easy to define recursive solution, to pass lists as arguments, to return several values ... and so it is generally unnecessary to use multiple functions, each of them calling the other ones, recursively modifying the shared global state. That's a mess and can't be debugged.

Related

how to return the value of a prime number while trying to print the sum of prime numbers in a range of numbers from check-prime to prime-sum

I was working on this question to print the sum of prime numbers in a range of numbers. The range values will be be taken as an input from the user. I got the syntax errors all cleared out but could not figure out how to return the value of the prime no. to the sum if it is true.
(defun check-prime(num)
(if (= num 1) nil)
;(setq i 2)
(loop for i from 2 to ( isqrt num) do
(if (= (mod num i) 0) nil t)
(= i (+ i 1))
)
)
(defun prime-sum(*begin* *end*)
(defvar *s* 0)
(defvar *i* *end*)
(loop for *i* from *end* to *begin* do
;i= 13
(if (check-prime *i*)
(= *s* (+ *s* *i* ) )
)
(= *i* (- *i* 1))
)
(write *s*)
)
(defun vals()
(print "enter the range1: ")
(defvar *begin* (read))
(print "enter the range2: ")
(defvar *end* (read))
(prime-sum *begin* *end*)
)
(print(vals))
;b =4
;e 13
Here is your function, cleaned up a bit and formatted in a conventional way:
(defun check-prime(num)
(if (= num 1) nil)
(loop
for i from 2 to (isqrt num)
do
(if (= (mod num i) 0) nil t)
(= i (+ i 1))))
There are multiple things that are incorrect, and they boil down to how are Lisp forms evaluated.
There is a special form progn in Common Lisp that is used to evaluate a list of expressions, with the following pattern:
(progn E1 E2 ... EN)
A lot of other functions in Lisp are described has having "an implicit progn", that's the case for defun which allows for more than one expression in its body, but since the progn doesn't appear literally it is implicit. The behavior is however always the same: each expression E1 to EN is evaluated in sequence, and evaluation of the whole progn are the values returned by the last expression, EN (that's where the N comes from in the name). In other words, the intermediate values from E1 etc. are discarded, the expressions are only useful for their side-effects.
In your code, that's the case for (if (= num 1) nil), which is an expression in the body of defun which is not the last one, and which doesn't even have side-effects. It is thus completely useless.
Likewise, in the loop macro, all that follows the do keyword are expressions that are evaluated for their side effects and do not contribute to the resulting value of the whole loop form.
In fact, loop is a bit complicated but here your don't have any accumulating clause in your loop (something like collect, append, sum etc. that would compute a result). Instead, your loop will always return nil.
Note also that for i from 2 to (isqrt num) takes care of incrementing i, you don't have to do it yourself like you tried in the do. Moreover, = is a comparison operator, in order to mutate i you would need to use setf instead: (setf i (+ i 1)), or (incf i) (which I repeat is unnecessary here).
To combine the first two expressions, I would write:
(if (= num 1) nil ...)
Where ... is the loop: because if is a functional if, it evaluates to whichever branch is selected by the condition.
And inside the loop, I would use never:
(loop for i from 2 to (isqrt num) never (= 0 (mod num i)))
The never clause makes the loop compute a boolean and halt as soon as the result is false.

function (OccurencesOfPrimes < list >) which counts the number of primes in a (possibly nested) list

I am working on problem to get the occurence of Prime in a list in lisp.
Input:
Write a function (OccurencesOfPrimes < list >) which counts the number of primes in a (possibly nested) list.
Output: Example: (OccurencesOfPrimes (((1)(2))(5)(3)((8)3)) returns 4.
I am using the below code but getting the error like:
(
defun OccurencesOfPrimes (list)
(loop for i from 2 to 100
do ( setq isPrime t)
(loop for j from 2 to i
never (zerop (mod i j))
(setq isPrime f)
(break)
)
)
(if (setq isPrime t)
(append list i)
)
)
)
LOOP: illegal syntax near (SETQ ISPRIME F) in
(LOOP FOR J FROM 2 TO I NEVER (ZEROP (MOD I J)) (SETQ ISPRIME F) (BREAK)
)
Any help.
It is important to keep the format consistent with the expected conventions of the language. It helps when reading the code (in particular with other programmers), and can help you see errors.
Also, you should use an editor which, at the minimum, keep tracks of parentheses. In Emacs, when you put the cursor in the first opening parenthesis, the matching parenthesis is highlighted. You can spot that you have one additional parenthesis that serves no purpose.
(
defun OccurencesOfPrimes (list)
(loop for i from 2 to 100
do ( setq isPrime t)
(loop for j from 2 to i
never (zerop (mod i j))
(setq isPrime f)
(break)
)
)
(if (setq isPrime t)
(append list i)
)
) ;; <- end of defun
) ;; <- closes nothing
In Lisp, parentheses are for the computer, whereas indentation is for humans. Tools can automatically indent the code according to the structure (the parenthesis), and any discrepancy between what indentation you expect and the one being computed is a hint that your code is badly formed. If you look at the indentation of your expressions, you can see how deep you are in the form, and that alone helps you understand the code.
Symbol names are dash-separated, not camlCased.
Your code, with remarks:
(defun occurences-of-primes (list)
;; You argument is likely to be a LIST, given its name and the way
;; you call APPEND below. But you never iterate over the list. This
;; is suspicious.
(loop
for i from 2 to 100
do
(setq is-prime t) ;; setting an undeclared variable
(loop
for j from 2 to i
never (zerop (mod i j))
;; the following two forms are not expected here according
;; to LOOP's grammar; setting IS-PRIME to F, but F is not
;; an existing variable. If you want to set to false, use
;; NIL instead.
(setq is-prime f)
;; BREAK enters the debugger, maybe you wanted to use
;; LOOP-FINISH instead, but the NEVER clause above should
;; already be enough to exit the loop as soon as its
;; sub-expression evaluates to NIL.
(break)))
;; The return value of (SETQ X V) is V, so here your test would
;; always succeed.
(if (setq is-prime t)
;; Append RETURNS a new list, without modifying its
;; arguments. In particular, LIST is not modified. Note that "I"
;; is unknown at this point, because the bindings effective
;; inside the LOOP are not visible in this scope. Besides, "I"
;; is a number, not a list.
(append list i)))
Original question
Write one function which counts all the occurrences of a prime number in a (possibly nested) list.
Even though the homework questions says "write one function", it does not say that you should write one big function that compute everything at once. You could write one such big function, but if you split your problem into sub-problems, you will end with different auxiliary functions, which:
are simpler to understand (they do one thing)
can be reused to build other functions
The sub-problems are, for example: how to determine if a number is a prime? how to iterate over a tree (a.k.a. a possibly nested list)? how to count
the occurrences?
The basic idea is to write an "is-prime" function, iterate over the tree and call "is-prime" on each element; if the element is prime and was never seen before, add 1 to a counter, local to your function.
You can also flatten the input tree, to obtain a list, then sort the resulting
list; you iterate over the list while keeping track of the last
value seen: if the value is the same as the previous one, you
already know if the number is prime; if the previous number differs, then
you have to test if the number is prime first.
You could also abstract things a little more, and define a higher-order tree-walker function, which calls a function on each leaf of the tree. And write another higher-order function which "memoizes" calls: it wraps around a
function F so that if you call F with the same arguments as before,
it returns the result that was stored instead of recomputing it.
Example
I'll combine the above ideas because if you give that answer to a teacher you are likely to have to carefully explain what each part does (and if you can, great for you); this is not necessarily the "best" answer, but it covers a lot of things.
(defun tree-walk-leaves (tree function)
(typecase tree
(null nil)
(cons
(tree-walk-leaves (car tree) function)
(tree-walk-leaves (cdr tree) function))
(t (funcall function tree))))
(defun flatten (tree &optional keep-order-p)
(let ((flat nil))
(tree-walk-leaves tree (lambda (leaf) (push leaf flat)))
(if keep-order-p
(nreverse flat)
flat)))
(defun prime-p (n)
(or (= n 2)
(and (> n 2)
(oddp n)
(loop
for d from 3 upto (isqrt n) by 2
never (zerop (mod n d))))))
(defun count-occurences-of-prime (tree)
(count-if #'prime-p (remove-duplicates (flatten tree))))
(count-occurences-of-prime '(((1)(2))(5)(3)((8)3)))
=> 4
If, instead, you don't want to remove duplicates but count the multiple times a prime number occurs, you can do:
(count-if (memoize #'prime-p) (flatten tree))
... where memoize is:
(defun memoize (function &key (test #'equalp) (key #'identity))
(let ((hash (make-hash-table :test test)))
(lambda (&rest args)
(let ((args (funcall key args)))
(multiple-value-bind (result exists-p) (gethash args hash)
(values-list
(if exists-p
result
(setf (gethash args hash)
(multiple-value-list (apply function args))))))))))
(memoize is useless if there are no duplicates)

Lisp: How to obtain the final loop value?

(defun index-iteration (n)
(let ((x 0))
(loop for j from 0 to n while (< x n) do
(setf x (max-index-for-iteration j)))))
I have the following lisp code, at the end of the loop I want to return the value j. I have investigated:
collect but that returns a list of values with the value I want (the last j at the end)
finally but it didn't work.
return but it ends the loop early
You need to combine finally and return like this:
(loop ...
finally (return (1- j)))
Please note the unfortunate use
of 1- which is due to the
fact that the finally clause is executed after the termination
clause (while in your case) transfers control there.
IOW, it requires the knowledge about how the termination clause is handled.
You can stay within the loop domain (without the extra let) like
this:
(loop for j from 0 to n
for ret = j
do ...
finally (return ret))
You could also use the maximize clause:
(loop for j from 0 to n ...
maximize j)
but this relies on the knowledge that the final value of j was the maximum one.
PS. Note also that you do not need the while clause in your loop.
It seems like you might want something like this, but I am unsure:
(defun index-iteration (n)
(loop :for j :from 0 :to n
:for x := (max-index-for-iteration j)
:while (<= x n)
:finally (return (1- j))))
There are several things that make this smell like off-by-one: the to, the <=, and in your question code the fact that x is always 0 during the first iteration (what should happen for n = 0?). Maybe show a bit more context, e. g. max-index-for-iteration, or what you want to achieve, for a better answer.

UVa 10120 Gift?! in Common Lisp?

I'm learning common lisp I've been given a problem out of the uVA database (http://acm.uva.es/p/v101/10120.html) and a breadth search function (which takes in a start point, goal point and a legal move generator), i've got the theory down as to how i'm meant to get the answer but Lisp just isn't agreeing with me. Can i have some advice on how to proceed from this point onwards? Below is a link to the given problem and my two of my attempted solutions with lisp source code. Any help would be greatly appreciated! Thanks!
1.
(defun gift (N G)
(setq CR 9)
(setq i 3)
(cond ((= N G) "N and G equal")
((< N G) "Gift it on a rock outside limits")
((> N 49) "number of rocks is bigger than 49 - it will work")
((< N 9) "number of rocks is less than 9, it wont work")
((= N 0) "number of rocks is 0, it wont work")
((= G 0) "gift isn't on a rock, it wont work"))
(loop
(setq I (+ I 1))
(setq I (-(* I 2) 1))
(setq CR 9)
(breadth-search CR G #'lmg-moves)
(when (= CR G) (return "Let me Try!"))
(when (> CR N) (return "Don't laugh at me!"))
))
(defun lmg-moves (I)
(list (+ 9 I)
(- 9 I)
))
2.
(defvar *currentRock* 9)
(defvar *iterator* 3)
(defun gift (N G)
(setq *iterator* (+ *iterator* 1))
;; (breadth-search *currentRock* G #'LMG)
)
(defun LMG (a)
(+ a (-(* *iterator* 2) 1))
)
As can be seen above, the general idea is to simply apply a breadth-search function with the given legal move generator and hopefully, by analizing it's output we can determine whether we can reach the goal state or not. I will be glad to answer any questions if the code above is too confusing, thanks again!.
Among other potential issues:
You're using LOOP wrong. See PCL for info on loop. I've rehacked it a bit, but I don't know what you are attempting.
SETF is recommended over SETQ, as SETF is more general.
INCF increments a place by 1.
Your indentation is bad; if you fixed that you would notice that you're falling off the end of COND into the LOOP. I'd recommend an auto-indenting editor for using Lisp here. (Emacs is the standby).
(defun gift (N G)
(setq CR 9)
(setq i 3)
(cond ((= N G) "N and G equal")
((< N G) "Gift it on a rock outside limits")
((> N 49) "number of rocks is bigger than 49 - it will work")
((< N 9) "number of rocks is less than 9, it wont work")
((= N 0) "number of rocks is 0, it wont work")
((= G 0) "gift isn't on a rock, it wont work")) )
(loop
while t
do
(setq I (+ I 1))
(setq I (-(* I 2) 1))
(setq CR 9)
(breadth-search CR G #'lmg-moves)
(when (= CR G)
(return "Let me Try!"))
(when (> CR N)
(return "Don't laugh at me!"))))
There are some things that are immediately obvious:
You have exactly two legal return values, "Let me try!", and "Don't make fun of me!". You misspelt the first, rephrased the second, and added a lot of strings that do not have a use for the problem (are they meant as comments?).
The description calls the variables N and M, but your attempts take parameters N and G. Why confuse yourself? Either call them N and M, or (better) use meaningful names, like rock-number and gift-place.
Now, let's see your program structure.
(defun gift (N G)
(setq CR 9)
(setq i 3))
These setq instructions have undefined behaviour at this point, because CR and I are not defined yet. Many Lisp implementations will implicitly create globally special variables of these names, but it is bad style to depend on it. I have the impression that you want to use let here, like this:
(defun gift (rock-number gift-place)
(let ((current-rock 0)
(jump-number 0))
;; ...
))
Note that you should really start from the beginning, because you would miss the solution when the gift is on rock 1 or 4.
Next up, that cond form: it is dead code, because it has no side effects, and you throw away its return value immediately. It is thus at best a comment, and you should use a comment for that.
Finally, we have this funny loop:
(loop
(setq I (+ I 1))
(setq I (-(* I 2) 1))
(setq CR 9)
(breadth-search CR G #'lmg-moves)
(when (= CR G) (return "Let me Try!"))
(when (> CR N) (return "Don't laugh at me!"))))
I don't know what breadth-search does, but it seems that you really depend on the manipulation of globally special variables. I cannot say what might happen here. However, I can see several problems:
You can have up to two locations when jumping a certain distance from a given rock. It cannot be right to check only a single variable after each jump.
You seem to confuse the jump number with its jump distance. I goes in the sequence 1, 3, 7, 15 …, but the jump number sequence would be 1, 2, 3, 4 … while the jump distance sequence would be 1, 3, 5, 7 …. Even the rocks visited when always jumping right are a different sequence (1, 4, 9, 16 …).
You reset CR to 9 each time through the loop. I do not see how that could be right.
Stylistically, you should keep your variables as local as possible, using for example let, do, or the extended loop keywords :for and :with, then pass them into the functions that need them as arguments. This makes it much easier to reason about what is happening.
I think that your mental model of the solution algorithm is a bit confused. I would structure this in such a way that you loop over the jumps and keep a set of rocks that you can possibly be on after exactly this number of jumps. A special treatment for small N does not seem to really give a lot of efficiency gain. If you have a proof that N > 49 always has a solution, on the other hand, you should have a guard clause and a comment that outlines the proof.

Lisp warning: xx is neither declared nor bound, it will be treated as if it were declared SPECIAL

I am new to lisp and am writing a few simple programs to get more familiar with it. One of the things I am doing is writing a recursive and iterative version of a factorial method. However, I have come across a problem and can't seem to solve it.
I saw a similar error at
Lisp: CHAR is neither declared nor bound
but a solution wasn't actually reached, other than the OP realized he made a "typing mistake". In the REPL I can use the setf function and it works fine. I am also using LispBox with emacs. I would appreciate any suggestions!
(defun it-fact(num)
(setf result 1)
(dotimes (i num)
(setf result (* result (+ i 1)))
)
)
WARNING in IT-FACT :
RESULT is neither declared nor bound,
it will be treated as if it were declared SPECIAL.
There are a few things wrong or not so good Lisp style:
(defun it-fact(num) ; style: use a space before (
(setf result 1) ; bad: variable RESULT is not introduced
(dotimes (i num)
(setf result (* result (+ i 1))) ; bad: extra addition in each iteration
) ; style: parentheses on a single line
) ; bad: no useful return value
A possible version:
(defun it-fact (num)
(let ((result 1)) ; local variable introduced with LET
(loop for i from 1 upto num ; I starts with 1, no extra addition
do (setf result (* result i)))
result)) ; result gets returned from the LET
In Lisp, local variables must be explicitly declared with LET or other forms that create local variables.
That differs from e.g. Python or JavaScript where assignment to variable creates the variable in current lexical scope.
Your example can be rewritten like this:
(defun it-fact(num)
(let ((result 1))
(dotimes (i num)
(setf result (* result (+ i 1))))))
An off-topic comment: there is no point in putting closing parentheses on separate lines.
You need to bind the variable 'result' - using 'let', for example - before starting to use it:
(defun it-fact(num)
(let ((result 1))
(dotimes (i num)
(setf result (* result (+ i 1))))))
For futher details you might want to read this ...