I wrote a function for recursive subtraction in lisp, but when i load it and run the function with numbers for example ( subtract 4 3 ) the output is -3 which I do not understand why.
(defun subtract (x y)
(if (eq y 0)
x
(- 1 (subtract x (- y 1) ) ) ) )
First, don't use eq to compare numbers. It accidentally works in this case, because your numbers happen to be small. Either use =, or (since you're looking for zero) zerop.
Second, you can use (trace subtract) to see a trace of invocations and return values.
Third, (- 1 x) and (- x 1) have, in the general case, very different values.
Here's a fixed version of your code:
(defun subtract (x y)
(if (zerop y)
x
(subtract (1- x) (1- y))))
Notice that in the recursive call we subtract 1 from both x (to actually decrement the number) and y (to get it closer to the base case, 0).
Related
New to common lisp and having a very rookie problem. My function of one variable is supposed to return the absolute of of the entered variable. It works for when the variable is above or equal to 0 but not below, I suspect this is due to the cond function but i'm not sure.
I have tried the code with brackets and without but cannot see why it is failing. I know this is not the best way of solving this problem but i am just trying to get used to the cond statement at this stage.
(defun abs-x (x)
(cond ((> x 0) x)
((= x 0) 0)
((< x 0) (-x))))
The error message when a variable below 0 is entered is '-X is undefined.
Use
(- x)
; ^
; |
; The space
; is important.
instead of (-x).
That's because - is a valid character in an identifier, so -x is a valid function name. With the space between - and x, though, it calls the function - which takes one or more arguments.
Shorter:
(defun abs-x (x)
(cond ((> x 0) x)
(t (- x))))
Reduced number of checkings using the fact that (- 0) also evaluates to0.
Instead of <= for the last check, use the simpler t - the else in common lisp cond clauses.
With if this would be:
(defun abs-x (x)
(if (> x 0)
x
(- x)))
I'm having a little trouble creating a recursive function in Scheme. I need to create a function called foo(x) that recursively does the addition of all the powers. For example foo(5) would be 5^4 + 4^3 + 3^2 + 2^1 + 1^0 = 701.
The stopping condition is if x = 0 then return zero. Else then return x^x-1 + foo(x-1)
Here's what I have so far for my function:
(define (foo x)
(cond ((zero? x) 0)
(else (+(expt(x (- x 1)))foo(- x 1)))))
You just have to be more careful with the parentheses, in particular notice that the correct way to call a procedure is like this: (foo x), instead of this: foo(x). This should work:
(define (foo x)
(cond ((zero? x) 0)
(else (+ (expt x (- x 1))
(foo (- x 1))))))
(foo 5)
=> 701
Allow me to ident the code. I just pasted it in DrRacket and hit CTRL+I then put the arguments to + on one line each:
(define (foo x)
(cond ((zero? x) 0)
(else (+ (expt (x (- x 1)))
foo
(- x 1)))))
So the base case is ok, but your default case looks very off. x is treated as a procedure since it has parentheses around it and - also uses x as if it's a number. It can't be both.
foo is not applied since it doesn't have parentheses around it so it evaluates to a procedure value, while + would expect all its arguments to be numeric.
The rules of Scheme are that parentheses matters. x and (x) are two totally different things. The first x can be any value, but (x) is an application so x have to evaluate to a procedure. Some exceptions are for special forms you need to know by heart like cond, and define but rather than that it's very important to know you change the meaning of a program by adding parentheses.
The correct definition of your procedure might be:
(define (foo x)
(if (zero? x)
0
(+ (expt x (- x 1))
(foo (- x 1)))))
(foo 5) ; ==> 701
Here I've changed cond to if since none of conds features were used. Seeing cond I expect either side effects or more than one predicate.
This is what I have:
(define (10th-power 10 y)
(if (= y 0)
1
(* 10 ((10th-power 10 (- y 1)))))
for example if I input 2 it should give out 1024.
There are a lot of errors in this short procedure. Here are the errors reported by racket:
read: expected a ')' to close '(' since you are missing ending parentheis
define: not an identifier... in 10 as 10 cannot be a variable name it cannot be in the argument list.
application: not a procedure. Double parentheses in the recursion part makes the result from 10th-power tried as a procedure as the result instead of just using the value as is.
If you fix those your procedure will work, but it will do the 10^y instead of y^10. Perhaps you need a helper where you keep how many times you have multiplied y that counts down instead of y which is the one that should be in 10's place.
You were close:
#lang racket
(define (10th-power y)
(if (= y 0)
1
(* 10 (10th-power (- y 1)))))
(10th-power 3)
Things to note: You can't insert an extra parenthesis around an expression. Example: (100) means call 100 with no arguments - and since 100 is not a function, you get the error "application: not a procedure:.
Second thing to note: You do not need the 10 as an argument.
you can write a recursion like this:
#lang racket
(define (10th-power y)
(if (= y 0 )
1
(* 10 (10th-power (- y 1)))))
by the way, if you want to improve you space efficiency from o(n) to o(1),you can write iteration:
#lang racket
(define (10th-power y)
(define (iter back times)
(if (= times 0)
back
(iter (* 10 back) (- times 1))))
(iter 1 y))
(10th-power 3)
I am struggling with understanding what I really need to do, and would like some outside input or a point to a good reference. I have been asked to use procedural representation to "implement sets of numbers." Each set will be a one argument function that takes a number and decides if the number is in the set. A few functions (that I have read can be defined in one line) that I have to create:
A function that returns a function taking a number as an argument and checks if the number is in the set.
A union function that returns the set of all elements in arg1 or arg2
Intersection function of above
Function that returns elements in arg1 but not arg2
etc.
Now I know this is simply enclosing a lambda function, but I guess I am confused on how to represent a set and check it within the lambda function? If someone could point me in the right direction I would appreciate it.
You have to realise that the first function in your TODO list is the constructor. The rest falls out from there I think.
(define (make-set x) (lambda (y) (eq? x y)))
(define (union x y) (lambda (z) (or (x z) (y z))))
(define (intersection x y) (lambda (z) (and (x z) (y z))))
(define (difference x y) (lambda (z) (and (x z) (not (y z)))))
(define set-5 (make-set 5))
(set-5 4)
(set-5 5)
(define set-45 (union (make-set 4) (make-set 5)))
(set-45 3)
(set-45 5)
(define set-34 (union (make-set 3) (make-set 4)))
(define set-4 (intersection set-34 set-45))
(set-4 3)
(set-4 5)
(set-4 4)
(define set-3 (difference set-34 set-45))
(set-3 4)
(set-3 5)
(set-3 3)
I am brand new to Scheme and am working on an assignment to implement stochastic gradient descent. So far I believe I have the structure of the program correct however my procedure that takes the derivative of a function f(x) is giving me some trouble. In my "try" loop at the bottom of the code I recursively call (try (func-eval guess)) where (func-eval guess) calculates the next guess of my function's local minimal with the formula *x - alpha*f'(x)* where alpha = 0.1.
I seem to be getting an error when calculating the derivative... I am using Dr.Racket IDE and it has highlighted this following line as being problematic:
(f (+ x dx)) ... which is the second line in my local derivative procedure:
(define (local-minimal first-guess)
;A way to check a guess
(define(good-enough? val1 val2)
(<(abs(- val1 val2)) 0.00001))
; x_new = x_old - alpha*f'(x) /// f(x)=(x+1)^2+2 //// alpha = 0.1
(define (func-eval x)
(- x (* 0.1((derivative (+ 2(expt (+ x 1) 2)) 0.00001)x))))
(define (derivative f dx)
(lambda (x)
(/ (- (f (+ x dx)) (f x))
dx)))
; trys the guess
(define (try guess)
(if (good-enough? guess -1)
guess
(try (func-eval guess))))
(try first-guess))
I am getting an error saying:
application: not a procedure;
expected a procedure that can be applied to arguments
given: 3
arguments...:
-1.99999
Is this a syntax error? I thought that I would be able to say f(x+dx) by using (f (+ x dx)) .... does this mean that I need to put an operator before the f in those parenthesis?
The highlighting and error message are together telling you something useful: the thing derivative is receiving as its first argument f isn't a function, which it needs to be to called in (f (+ x dx)). Where does the argument come from? We could run DrRacket's debugger, but here we can just look at the code -- the only place derivative is called from is the first line of func-eval, so that's where we must have passed a number instead of a function. Sure enough, (+ 2 (expt (+ x 1) 2)) (with x bound) is just a number, and trying to apply this gives an error.
When calling derivative, the first argument has to be a function. In the following procedure call, the expression in the first argument gets evaluated to a number, not a function:
(derivative (+ 2 (expt (+ x 1) 2)) 0.00001)
To fix it, pack the expression inside a lambda, which makes it an actual function:
(derivative (lambda (x) (+ 2 (expt (+ x 1) 2))) 0.00001)