I need to know the remainder of a division:
(remainder (/ 3 2) 2)
But as (/ 3 2) is not integer gives me an error.
modulo: contract violation
expected: integer?
given: 1.5
argument position: 1st
How could I solve it?
Aren't you just looking for
(remainder 3 2)
?
If you want to get back a float, one of the operands needs to be a float, e.g. (/ 3.0 2) => 1.5.
If you can't change the numbers inside the division, you can use exact->inexact.
(exact->inexact (/ 3 2)) => 1.5
If you're trying to get the remainder, have you thought about the modulo operator?
Modulo is basically the number 'left over', or the overflow.
(modulo 3 2)
=> 1
(modulo 3 1)
=> 0
To determine whether a number is even or not, use even?.
An integer is even if the remainder is zero, when the number is divided by 2.
A non-integer is not even.
(define (my-even? x)
(if (integer? x)
(= (remainder x 2) 0)
#f)))
The builtin functions is called even?.
> (even? 5)
#f
Related
The problem I have to solve is how many oranges are in a pyramid if each level has 2^n oranges. I think I have the basis for the solution, however this is an infinite recursion. How do I get out of this function when n has reached -1 and display the solution? This is in scheme.
I've used this to help me set up what I need:
(+ (expt 2 0) (+ (expt 2 1) (+ (expt 2 2) (expt 2 3))))
3 is arbitrary and I only used it to help me write out the solution
(define oranges
(lambda (n)
(+ (expt 2 n) (oranges(- n 1)))))
Running this will not work because it is an infinite loop.
When you're writing a recursive procedure, it's mandatory to have a base case: it's the exit point of the recursion, otherwise it'll loop infinitely. Ask yourself: when should my procedure end? what should I return in that case?
In your case is simple, you literally wrote the answer in the title: how should we handle the case when n = -1? (or -2, or -3...?) Just return a meaningful value! I'd suggest 0, because we're doing an addition.
Now, in your code ask with an if expression whether we're in a value when we should return, and then return 0 - otherwise do the recursive step, which will eventually reach the base case. This is what I mean:
(define oranges
(lambda (n)
(if (< n 0)
0
(+ (expt 2 n) (oranges (- n 1))))))
It works as expected:
(oranges 3)
=> 15
When you make a recursive function, there are different parts that are compulsory. You need the general case and the basic cases. You have forgotten the basic cases also known as exit condition. Your exit condition is n = 0, so your program will be:
(define (oranges n)
(cond [(= n 0) 1]
[else (+(expt 2 n)(oranges(- n 1)))]
)
)
Tested:
(oranges 1)
=> 3
(oranges 0)
=> 1
(oranges 2)
=> 7
The following function was given to me on a review sheet:
(define mystery(lambda(m n)
(cond
((= m 0) n)
((= n 0) m)
(#t (+ 2(mystery(- m 1)(- n 1))))
)))
The first two conditions are simple, it's just the recursive otherwise that's confusing me. It just seems to me that the recursion will continue until they both equal zero, which certainly doesn't return the sum. Can someone provide an explanation?
First, let's format the code a bit better to see what's happening:
(define (mystery m n)
(cond ((= m 0) n)
((= n 0) m)
(else (+ 2 (mystery (- m 1) (- n 1))))))
Now, remember that a cond executes only the action corresponding to the first condition that is true (from top to bottom), the others are ignored. If none of the conditions is true, then the else part is executed. The important thing to remember is that only one action is executed.
In particular, your mystery procedure will stop when either m or n becomes zero, not when both become zero. When one of the two reaches zero, the recursion starts to unwind, returning the sum. You can see this when tracing the execution - for example, in Racket:
(require racket/trace)
(trace mystery)
(mystery 3 2)
>(mystery 3 2)
> (mystery 2 1)
> >(mystery 1 0)
< <1
< 3
<5
Just to elaborate on Óscar López's answer (I can't format this in a comment): I find that it's often useful to write these sorts of little recursive maths functions down as if they were maths:
Let m and n be natural numbers, then
n + m = n if m = 0;
n + m = m if n = 0;
n + m = n - 1 + m - 1 + 2;
there are no other cases.
I feel the best way is not to nest but to precompute. Looking at the base case we test with either zero:
(mystery 0 2) ; ==> 2
(nystery 3 0) ; ==> 3
Thus every time at least one argument is zero it returns the other argument. Lets try with a non zero value and remember the second you see a value we have already done before you just switch it with its result:
(mystery 1 3) ; ==
(+ 2 (mystery 0 2)) ; == (we switch known value)
(+ 2 2)
; ==> 4
(mystery 4 1) ; == (we substitute with the expression)
(+ 2 (mystery 3 0)) ; == (we switch known value)
(+ 2 3)
; ==> 5
Since we know the base case always returns the other value we don't need to precalculate it. Here is a go that does that:
(mystery 3 9) ; == (we substitute with the expression)
(+ 2 (mystery 2 8) ; == (we substitute with the expression)
(+ 2 (+ 2 (mystery 1 7))) ; == (we substitute with the expression)
(+ 2 (+ 2 (+ 2 (mystery 0 6))) ; == (we substitute with the expression, n, which is 6)
(+ 2 (+ 2 (+ 2 6))) ; == (we substitute (+ 2 6))
(+ 2 (+ 2 8)) ; == (we substitute (+ 2 8))
(+ 2 10) ; == (we substitute (+ 2 10)
; ==> 12
We can generalize what will happen. The lowest of n and m will decide when the recursion ends. At each step it will add 2 and recurse. Thus it is a fancy way of making:
(define (double-min n m)
(let ((vmin (min n m))
(vmax (max n m)))
(+ (* 2 vmin) (- vmax vmin))))
Which again is a fancy way of adding the two numbers since if n > m, then 2*m+(n-m) = m+m+(n-m) = m+n
(define mystery(lambda(m n)
(cond
((= m 0) n)
((= n 0) m)
(#t (+ 2 (mystery (- m 1) (- n 1))))
)))
First and second conditions are obvious.
Explanation of how the third statement is working:
1 each is taken out of m and n and kept as 2 outside this function.
This is continued till either m is 0 or n is 0.
The first 2 cases are obvious, the sum of 2 numbers where one of the numbers is 0, is equal to the other number.
In the last case, after checking the arguments for 0, we know for sure that both of them are non-0. Assuming that mystery returns the sum of its 2 arguments, then
(+ 2 (mystery (- arg1 1) (- arg2 1)))
will return a sum that is equal to (mystery arg1 arg2) and will eventually halt when one of the arguments is 0, returning the desired result.
Assuming that mystery returns the sum of its 2 arguments is key here and is called the recursive leap of faith. (Google it)
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).
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 need to make a recursive procedure that finds how many 6's are in a number. For example, 606 has two 6's. I started it.
(define num
(lambda (n)
(cond
((< n 0) (num (- n)))
((= n 0) 0)
((> n 6)
I do not want to convert anything. Is there a way of dividing it by 10, then if the decimal is .6 add one?
1) For integer division in scheme, use the quotient function, as in (quotient 9 2) gives 4.
2) For integer modulus in scheme, use the remainder function, as in (remainder 9 2) gives 1.
Scheme is based on recursion, as you stated. To solve a recursive problem, you have to state the solution in terms of a smaller instances of the problem:
"The number of 6 digits in a number N is ... "
and for ... you state the solution in terms of the solution of part of N.
The easiest part of N to recur on is N/10, which is all but the last digit of N. Then, to solve this recursively, you should assume that you know the answer to (num (quotient N 10)). Call that value X. How does knowing N and X tell you how many digits is in N?
If the last digit of N is 6, then the solution is X+1. Otherwise, the solution is X. How can you tell if the last digit is 6? Use the remainder function.
(define (num N)
; check cases
(cond
; case 1) negative condition
((< N 0) (num (- N)))
; case 2) zero condition, the terminal case
((= N 0) 0)
; case 3) recursive case with a 6 as the last digit
((= (remainder N 10) 6) (+ X 1))
; case 4) recursive case without a 6 as the last digit
(#t X)
))
Now you just have to substitute the recursive assumption in for X, so for example case 3 becomes
; case 3
((= (remainder N 10) 6) (+ (num (quotient N 10)) 1))
Change X for case 4 as well and you'll get the solution.
Almost as good, let me introduce you to modulo. It takes in two numbers, and returns the remainder of the first divided by the second. Taking the (modulo n 10) will return the last digit of n. The general Scheme (ba-duh-duh-chiiing) for what you want is
(define num-6s
(lambda (n)
(cond
((< n 10)
; Base Case, if n = 6, then 1 else 0
((= (modulo n 10) 6)
; The last digit of n is 6
(else
; The last digit of n isn't 6
))))
Now as another helpful hint, (floor (/ n 10)) drops the last digit of a number. This is how you should get the next number to recurse upon.