I'd like to have a function that does the following recursively
;f(n) = n, if n<4
;f(n) = f(n-1) + 2f(n-2) + 3f(n-3) + 4f(n-4)
This is what I came up with but it does not cover the multiplication at each recursive call. I was wondering how one would implement that with Scheme
(define (function n)
(= counter 0)
(if (< n 4)
n
(+ n (function (- n 1)))))
What you currently have written is:
f(n) = n, if n<4
f(n) = n + f(n-1)
With an out of place, and completely useless (= counter 0) statement which can only return #t or #f (This will give a runtime error if counter is not defined within the scope).
The below code will do what you are trying to acheive. If you want to convert a recursive mathmatical definition to code, you need to have as many recursive funtion calls in the code as you have in the mathmatical definition (in this case, 4).
(define (function n)
(if (< n 4)
n
(+ (* 1 (function (- n 1))) (* 2 (function (- n 2))) (* 3 (function (- n 3))) (* 4 (function (- n 4))))))
Related
I'm pretty new to Common Lisp. And I try to build my own operator functions.
In the first function I tried to add one to the given number.
The second function we do a recursive use of the first in the frequency of m.
When I enter totaladd ( 5 3 ) I expect an 8.
What can I do about the undefined funciton k?
(defun add1(n)
(+ n 1)
)
(write (add1 5))
(defun totaladd (k m)
(if (eq m 0)
0
(totaladd(add1(k) (- m 1)))
)
)
(write (totaladd 5 3))
There are three errors in the next line:
(totaladd(add1(k) (- m 1)))
Let's look at it:
(totaladd ; totaladd is a function with two parameters
; you pass only one argument -> first ERROR
(add1 ; add1 is a function with one parameter
; you pass two arguments -> second ERROR
(k) ; K is a variable, but you call it as a function,
; but the function K is undefined -> third ERROR
(- m 1)))
(defun add1 (n) (+ n 1))
(defun totaladd (k m)
(if (= m 0)
k
(add1 (totaladd k (- m 1)))))
There is a extra function for (= ... 0) called zerop which asks whether a number os zero or not. Very frequently used when recursing over numbers as the break condition out of the recursion.
There is also an extra function for (- ... 1) or (+ ... 1) because these are common steps when recursing with numbers: (1- ...) and (1+ ...), respectively.
(Their destructive forms are (incf ...) and (decf ...), but these are not needed for recursion.)
So, using this, your form becomes:
(defun totaladd (k m)
(if (zerop m)
k
(add1 (totaladd k (1- m)))))
Write in Scheme a recursive function er, and a non-recursive (based on do-loop) function ei, that take as their argument the number of components n, and compute the following sum (approximation of e) 1 + 1/1! + 1/2! + 1/3! + ... + 1/n!, n>0
I have a solution for you. I hope that will help you!
((lambda (s) (s s -1 1 0))
(lambda (hep M f! euler-number)
((lambda (s)
(if (= M 20)
(+ 0.0 euler-number)
(s s 1 1 (+ euler-number (/ 1 f!)))))
(lambda (hop N x! euler)
(hep hop (+ N 1) (* x! N) euler)))))
First thing you have to do is to calculate the factorial, since you are going to need need in the for loop. Then, in the for loop you have to call the factorial function and please see how to do it with a for loop, instead of recursively. The last line of the post below is how to test for the function, which should return the correct number for the factorial of 6.
(define (factorial n)
(cond
((= n 0) 1)
((* n (factorial (- n 1))))))
(define (ei n)
(define sum 0)
(do ((i 0 (+ 1 i)))
((> i n))
(set! sum (+ sum (/ 1. (factorial i)))))
sum)
;(ei 6)
I trying to write simple factorial function in clojure, but i am getting this error:
java.lang.Long cannot be cast to clojure.lang.IFn
I know this error is usually due to an extra parenthesis, but I am not sure about this case.
First I wrote function in LISP, and it works as it should.
Code:
(defun factorial (n)
(if (= n 1)
1
(* (factorial (1- n)) n )
)
)
(factorial 5)
Then I tried it in clojure, where it doesn't work.
Clojure code:
(defn factorial [n]
(if (= n 1)
1
(* (factorial(n)) n)
)
)
(defn -main
[& args]
(println(factorial 5))
)
You've got an extra set of parens in your recursive call to factorial, probably because you meant to decrement n, it should be
(defn factorial [n]
(if (= n 1)
1
(* (factorial (dec n)) n) ;; <==
)
)
As MarkNFI showed decrementing has its own operator inc.
But to show you the problem in your code:
(defun factorial (n)
(if (= n 1)
1
(* (factorial (1- n)) n ) ;; (1- n) must be changed to (dec n) or (- n 1)
)
)
(factorial 5)
(1- n) is not the way operators in clojure work. You have to place the operator first. So in your case it would be: (- n 1)
I have the following recursive function that I need to convert to iterative in Scheme
(define (f n)
(if (< n 3) n
(+
(f (- n 1))
(* 2 (f(- n 2)))
(* 3 (f(- n 3)))
)
))
My issue is that I'm having difficulty converting it to iterative (i.e. make the recursion have linear execution time). I can think of no way to do this, because I just can't figure out how to do this.
The function is defined as follows:
f(n) = n if n<3 else f(n-1) + 2f(n-2) + 3f(n-3)
I have tried to calculate it for 5 linearly, like so
1 + 2 + f(3) + f(4) + f(5)
But in order to calculate say f(5) I'd need to refer back to f(4), f(3), f(2) and for f(4) Id have to refer back to f(3), f(2), f(1)
This is a problem from the SICP book.
In the book, authors have an example of formulating an iterative process for computing the Fibonacci numbers.
(define (fib n)
(fib-iter 1 0 n))
(define (fib-iter a b count)
(if (= count 0)
b
(fib-iter (+ a b) a (- count 1))))
The point here is that use two parameter a and b to memorise f(n+1) and f(n) during computing. The similar could be applied: we need a, b, c to memorise f(n+2), f(n+1) and f(n)
;; an interative process implementation
(define (f-i n)
;; f2 is f(n+2), f1 is f(n+1), f0 is f(n)
(define (interative-f f2 f1 f0 count)
(cond
((= count 0) f0)
(else (interative-f
(+ f2 (* f1 2) (* f0 3))
f2
f1
(- count 1)))))
(interative-f 2 1 0 n))
I want to program a function to find C(n,k) using tail recursion, and I would greatly appreciate your help.
I have reached this:
(defun tail-recursive-binomial (n k)
(cond ((or (< n k) (< k 0)) NIL)
((or (= k 0) (= n k)) 1)
(T (* (tail-recursive-binomial (- n 1) (- k 1)) (/ n k)))))
Using the following property of the binomial coefficients.
But I don't know how to make the recursive call to be the last instruction executed by each instance, since there the last one is the product. I have been trying it by using an auxiliary function, which I think is the only way, but I haven't found a solution.
As starblue suggests, use a recursive auxiliary function:
(defun binom (n k)
(if (or (< n k) (< k 0))
NIL ; there are better ways to handle errors in Lisp
(binom-r n k 1)))
;; acc is an accumulator variable
(defun binom-r (n k acc)
(if (or (= k 0) (= n k))
acc
(binom-r (- n 1) (- k 1) (* acc (/ n k)))))
Or, give the main function an optional accumulator argument with a default value of 1 (the recursive base case):
(defun binom (n k &optional (acc 1))
(cond ((or (< n k) (< k 0)) NIL)
((or (= k 0) (= n k)) acc)
(T (binom (- n 1) (- k 1) (* acc (/ n k))))))
The latter option is slightly less efficient, since the error condition is checked in every recursive call.
You need an auxiliary function with an extra argument, which you use for computing and passing the result.