I'm very new to lisp and I am working on basic syntax. I am trying to convert:
r1 = (-b + sqrt(b^2 - 4*a*c))/(2*a)
into a lisp format. The only problem I think I am having is that I cannot get lisp to recognize -b as the negative value of my symbol b. This is what I have so far from the lisp prompt:
[17]> (setq a 1L0)
1.0L0
[18]> (setq b -1L0)
-1.0L0
[19]> (setq c -1L0)
-1.0L0
[20]> (setq r1 (+ (/ (sqrt (- (power b 2) (* (* 4 a) c))) (* 2 a)) -b))
*** - EVAL: variable -B has no value
The following restarts are available:
USE-VALUE :R1 You may input a value to be used instead of -B.
STORE-VALUE :R2 You may input a new value for -B.
ABORT :R3 Abort main loop
use
(- b)
to negate b. It is equivalent to
(- 0 b)
Related
I know the algorithm :
(defun Hanoi (n) (if (= n 1) 1 (+ (* 2 Hanoi(- n 1)) 1)))
However, CLISP said
*** - IF: variable HANOI has no value
How could I tweak this? I can't find out the problem.
Here is your code:
(defun Hanoi (n)
(if (= n 1)
1
(+ (* 2 Hanoi (- n 1))
1)))
The error says:
*** - IF: variable HANOI has no value
So, inside the IF you are making a reference to HANOI as a variable, and there is no such variable currently bound at this point. If we look closely, we can see the following expression:
(* 2 Hanoi (- n 1))
This expressions is the application of function * to 3 arguments, namely 2, Hanoi and (- n 1). The second argument, Hanoi, stands for a variable. But there is no such variable defined here (for example, with a let). You are supposed to call the funtion #'Hanoi using the same syntax as you did for multiplication or addition, which is: wrap the function's name in parentheses along with its arguments:
(hanoi (- n 1))
See https://common-lisp.net/documentation for resources about Common Lisp.
You don't call functions as f(x) in Lisp, you call them as (f x) So your recursive call to hanoi needs to be (hanoi (- n 1)).
Just started learning lisp. I have no idea why I am getting these errors or even what they mean. I am simply trying to code an approximation of pi using the Gregory-Leibniz series, here is the code.
(defun gl (n)
(defparameter x 0) ;init variable to hold our runnning sum
(loop for y from 0 to n ;number of iterations, starting from 0 to desired n
(if (= y 0) ;if n is 0 then we just want 4
(defparameter w 4))
(if (> y 0) ;else, 4*(-1^y)/((2 * y)+1)
(defparameter w (* 4 (/ (exp -1 y) (+ (* 2 y) 1)))))
(+ x w)) ;add to our running sum
(write x)) ;once loop is over, print x.
I have tried using setq, defvar, let etc. instead of defparameter but I still get "Undeclared free variable X".
I also get the error "Unused lexical variable N" even though I am using it for my loop, which is weird also.
How can I fix this and why is it happening? Thanks!
Here is the code after Emacs auto-indented it:
(defun gl (n)
(defparameter x 0)
(loop for y from 0 to n
(if (= y 0)
(defparameter w 4))
(if (> y 0)
(defparameter w (* 4 (/ (exp -1 y) (+ (* 2 y) 1)))))
(+ x w))
(write x))
Compiling the following code with SBCL gives one error and two warnings.
One warning says that x is undefined.
You should not call defparameter from inside your function, since defvar and defparameter are used to declare dynamic variables and to set their value in the global scope. Prefer to have let bindings, or, since you already are using a loop, a with clause. When you want to modify a binding, use setf.
The errors comes from the macroexpansion of LOOP, which is malformed. For SBCL, that means that the code is treated as dead-code for the rest of the function compilation; that explains why n appears not to be used, which is what the second warning is about.
There are various fixes remaining to be done:
Use function EXPT, not EXP.
Calling (+ x w) only computes a value but does not modify x, the result is useless.
Prefer using if as expression, like a ternary operator in other languages, in your case the code can be simplified
Adding one can be done with function 1+ (that's the name of the function, not a special syntax for adding constants)
The write operation is rarely needed, especially if you are computing a mathematical formula; just return the value, and the REPL will print it automatically.
Small corrections that make your code works:
(defun gl (n)
(let ((x 0))
(loop
for y from 0 to n
for w = (if (= y 0)
4
(* 4 (/ (expt -1 y) (+ (* 2 y) 1))))
do (setf x (+ x w)))
(write x)))
I would personally get rid of x and w, and use a SUM loop clause.
(defun gl (n)
(loop
for y from 0 to n
sum (if (zerop y)
4
(* 4 (/ (expt -1 y)
(1+ (* 2 y)))))))
I am trying to print the value of an array in CLISP, I wrote below function:
(setq x (make-array '5 :initial-contents '(a b c d e)))
(loop for i from 0 to 4 do (write aref x i))
But I am getting error
*** - PROGN: variable AREF has no value
The following restarts are available:
USE-VALUE :R1 Input a value to be used instead of AREF.
STORE-VALUE :R2 Input a new value for AREF.
I am totally new to CLISP and not getting why I am getting the above error.
AREF is a function, not a variable.
You are missing a set of parentheses around it: (aref x i):
(loop for i from 0 to 4 do (write (aref x i)))
or just
(loop for e across x do (write e))
PS. Note that write is a
relatively low level function.
You probably want to use a variant of print or princ.
I am very new to ACL2 so I understand that some of you may feel this is such a simple solution that you would frown upon my outreach for help. I am trying to figure out how to get my code to add up to an Nth reciprocal squared (I.E if n=4 then i am looking for 1/1 + 1/4 + 1/9 + 1/16)
I have a function that will add up to n and it works and looks like this
(defun sum-up-to-n (n)
(if (zp n)
0
(+ n (sum-up-to-n (- n 1)))))
With the the Reciprocal squared looking like this
(defun sum-up-to-nSqRecip (n)
(if (zp n)
0
(+ (sum-up-to-nSqRecip (- n 1))) 1/n^2) ))
I receive this error "The body of
SUM-UP-TO-NSQRECIP contains a free occurrence of the variable symbol
|1/N^2|. Note that |1/N^2| occurs in the context of condition (NOT (ZP N))
from a surrounding IF test." and i do not know how to resolve this error.
included stuff
(encapsulate nil
(set-state-ok t)
(program)
(defun check-expect-fn (left right sexpr state)
(if (equal left right)
(mv nil (cw "check-expect succeeded: ~x0~%" sexpr) state)
(er soft nil "check-expect failed: ~x0
Expected: ~x1
Actual: ~x2" sexpr right left)))
(defmacro check-expect (&whole sexpr left right)
`(check-expect-fn ,left ,right (quote ,sexpr) state))
(logic))
(include-book "doublecheck" :uncertified-okp t :dir :teachpacks)
(include-book "arithmetic-5/top" :uncertified-okp t :dir :system)
ACL2 uses LISP syntax, which means you need prefix operators. So 1/n^2 should be (/ 1 (* n n)).
LISP allows a lot of the characters to be in a name, 1/n^2 in your example is treated as a name of a variable, which isn't binded to anything (not an input either). This is why you are receiving the "free occurrence of the variable" error.
Define a function "power" that takes two input arguments m and n, and returns m^n. Then, by using the function "power", define a function sum_power that takes two input arguments m and n and returns the sum: (1^n + 2^n + 3^n +.... + m^n).
int first function i calculate power from given arguments in second function a sum powers. But program gives error: Program stack overflow. RESET...
I cant find my error. Function power is correct I checked.
(defun power(m n)
(let ((result 1))
(dotimes (count n result)
(setf result (* m result)))))
(defun sum_power (m n)
(if (= 0 m)
0
(+ (powern m)
(sum_power (1- m) n))))
Ok the problem is your function sum_power when you pass the variable m
the expression (- 1 m) is an infinite loop first time because is for example
for m = 5 first time (- 1 m) => -4 (new m = -4) second time (- 1 m)
=> 5 (new m = 5)
begin again so is a recusive infinite loop, you never arrive to 1 so this is teh case of the overflow
Use instead the build function (1- m) which decreases the value of m, or (- m 1) if you want
So the new funtion will be like this, also this is no tail recursion so for big m and n it will take a lot of time, but for your needs it should work, and this function is better formated, please take of fromatting when writing lisp functions for easy reading
(defun sum_power (m n)
(if (= 1 m)
1
(+ (power n m) (sum_power (1- m) n))
You have a argument order mistake in your sum_power function. The stand lisp function - when given two arguments subtracts the second argument from the first argument that is (- 1 m) will subtract m from one and NOT 1 from m as you probably expected thus your result will be a negative integer and your base case (= 1 m) will never be reached. For you code to work correctly you have to swap the arguments to - thus (- m 1) or you can user the lisp function 1- (1- m) which subtracts one from its only argument thus a corrected version of your code is as follows:
(defun sum_power (m n)
(if (= 1 m)
1
(+ (power m n) (sum_power (1- m) n))))
On a non-related side-note lisp allows a lot more characters in function, variable and macro names than most other languages thus you can use sum-power instead of sum_power, in-fact it is arguably better lisp style to use hyphens to join multiple-word identifier names rather than underscores (as used in C) or camel-back casing (sumPower as used in Java). Secondly closing parenthesis usually are not written on a separate line but are on the same line as the last expression before the closing parentheses, as I have done above in the corrected version. These are merely conventions you may follow them if you wish but you're not obliged to.
Why you don't use higher-order functions and loop macro? I think is more readable that way:
(defun power (n m)
(reduce #'* (loop for x below n collect m))
(defun sum-power (n m)
(reduce #'+ (loop for x from 1 to m collect (power x n)))